Skip to content

Commit

Permalink
Ast
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Jun 13, 2023
1 parent 1eb6849 commit f3f539c
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/ast/ast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Statement } from './statement'
import { Pattern } from './match'

export type AstNodeKind =
'module'
| 'var-def'
| 'fn-def'
| 'kind-def'
| 'impl-def'
| 'type-def'
| 'return-stmt'
| 'unary-expr'
| 'binary-expr'

export interface Module {
type: 'module'
statements: Statement[]
}

export interface Type {
type: 'type'
name: Identifier
typeParams: Type[]
}

export type Identifier = string

export interface Param {
type: 'param'
pattern: Pattern
paramType?: Type
}

87 changes: 87 additions & 0 deletions src/ast/expr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { BinaryOp, UnaryOp } from './op'
import { Identifier, Param, Type } from './ast'
import { Block } from './statement'
import { MatchExpr, Pattern } from './match'

export type Expr = UnaryExpr | BinaryExpr

export interface UnaryExpr {
type: 'unary-expr'
unaryOp: UnaryOp
operand: Operand
}

export interface BinaryExpr {
type: 'binary-expr'
binaryOp: BinaryOp
lOperand: Operand
rOperand: Operand
}

export type Operand =
IfExpr
| WhileExpr
| ForExpr
| MatchExpr
| ClosureExpr
| Expr
| ListExpr
| StringLiteral
| CharLiteral
| IntLiteral
| FloatLiteral
| Identifier

export interface IfExpr {
type: 'if-expr'
condition: Expr
thenBlock: Block
elseBlock?: Block
}

export interface WhileExpr {
type: 'while-expr'
condition: Expr
block: Block
}

export interface ForExpr {
type: 'for-expr'
pattern: Pattern
expr: Expr
block: Block
}

export interface ClosureExpr {
type: 'closure-expr'
name: Identifier
typeParams: Type[]
params: Param[]
block: Block
returnType?: Type
}

export interface ListExpr {
type: 'list-expr'
exprs: Expr[]
}

export interface StringLiteral {
type: 'string-literal'
value: string
}

export interface CharLiteral {
type: 'char-literal'
value: string
}

export interface IntLiteral {
type: 'int-literal'
value: string
}

export interface FloatLiteral {
type: 'float-literal'
value: string
}
26 changes: 26 additions & 0 deletions src/ast/match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Expr } from './expr'
import { Block } from './statement'

export type Pattern = ConPattern | Expr | Hole

export interface ConPattern {
type: 'con-pattern'
// TODO
}

export interface Hole {
type: 'hole'
}

export interface MatchExpr {
type: 'match-expr'
expr: Expr
clauses: MatchClause
}

export interface MatchClause {
type: 'match-clause'
pattern: Pattern
block: Block
guard?: Expr
}
4 changes: 4 additions & 0 deletions src/ast/op.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type UnaryOp = 'todo'

export type BinaryOp = 'todo'

46 changes: 46 additions & 0 deletions src/ast/statement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Identifier, Param, Type } from './ast'
import { TypeDef } from './type-def'
import { Expr } from './expr'
import { Pattern } from './match'

export type Statement = VarDef | FnDef | KindDef | ImplDef | TypeDef | ReturnStmt | Expr

export interface VarDef {
type: 'var-def'
pattern: Pattern
varType: Type
expr: Expr
}

export interface FnDef {
type: 'fn-def'
name: Identifier
typeParams: Type[]
params: Param[]
block?: Block
returnType?: Type
}

export interface KindDef {
type: 'kind-def'
name: Identifier
kindParams: Type[]
block: Block
}

export interface ImplDef {
type: 'impl-def'
name: Identifier
implParams: Type[]
forKind?: Type
block: Block
}

export interface ReturnStmt {
type: 'return-stmt'
}

export interface Block {
type: 'block'
statements: Statement[]
}
20 changes: 20 additions & 0 deletions src/ast/type-def.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Identifier, Param, Type } from './ast'

export interface TypeDef {
type: 'type-def'
name: Identifier
typeParams: Param[]
variants: TypeCon[]
}

export interface TypeCon {
type: 'type-con'
fieldDefs: FieldDef[]
}

export interface FieldDef {
type: 'field-def'
name: Identifier
fieldType: Type
}

0 comments on commit f3f539c

Please sign in to comment.