more CG syntax. parsing the easy stuff

This commit is contained in:
Dustin Swan 2026-02-01 00:03:32 -07:00
parent 71237d0307
commit 1ed325e98b
No known key found for this signature in database
GPG key ID: 30D46587E2100467
3 changed files with 204 additions and 82 deletions

View file

@ -1,35 +1,24 @@
import type { Value } from './types';
// Literals and Variables
export type Literal = {
kind: 'literal'
value: Value
}
export type BinaryOp = {
kind: 'binaryop'
operator: string
left: AST
right: AST
}
export type Variable = {
kind: 'variable'
name: string
}
export type Let = {
kind: 'let'
export type Constructor = {
kind: 'constructor'
name: string
value: AST
body: AST
}
export type If = {
kind: 'if'
condition: AST
then: AST
else: AST
}
// Functions
export type Lambda = {
kind: 'lambda'
@ -43,4 +32,93 @@ export type Apply = {
args: AST[]
}
export type AST = Literal | BinaryOp | Variable | Let | If | Lambda | Apply
// Bindings
export type Let = {
kind: 'let'
name: string
value: AST
body: AST
}
// Matching
export type Match = {
kind: 'match'
expr: AST
cases: MatchCase[]
}
export type MatchCase = {
pattern: Pattern
result: AST
}
export type Pattern =
| { kind: 'wildcard' }
| { kind: 'var', name: string }
| { kind: 'literal', value: number | string }
| { kind: 'constructor', name: string, args: Pattern[] }
| { kind: 'list', elements: Pattern[] }
| { kind: 'record', fields: { [key: string]: Pattern } }
// Data Structures
export type Record = {
kind: 'record'
fields: { [key: string]: AST }
}
export type RecordAccess = {
kind: 'record-access'
record: AST
field: string
}
export type RecordUpdate = {
kind: 'record-update'
record: AST
updates: { [key: string]: AST }
}
export type List = {
kind: 'list'
elements: AST[]
}
// Top-level constructs
export type Definition = {
kind: 'definition'
name: string
body: AST
// type?: string // TODO
}
export type TypeDef = {
kind: 'typedef'
name: string
variants: Array<{ name: string, args: string[] }>
}
export type Import = {
kind: 'import'
module: string
items: string[] | 'all'
}
export type AST =
| Literal
| Variable
| Constructor
| Lambda
| Apply
| Let
| Match
| Record
| RecordAccess
| RecordUpdate
| List
| Definition
| TypeDef
| Import