From f3f539c7f6c2e5b0585051fcb9202777632f0490 Mon Sep 17 00:00:00 2001 From: ivanjermakov Date: Wed, 14 Jun 2023 01:56:36 +0200 Subject: [PATCH] Ast --- src/ast/ast.ts | 33 +++++++++++++++++ src/ast/expr.ts | 87 ++++++++++++++++++++++++++++++++++++++++++++ src/ast/match.ts | 26 +++++++++++++ src/ast/op.ts | 4 ++ src/ast/statement.ts | 46 +++++++++++++++++++++++ src/ast/type-def.ts | 20 ++++++++++ 6 files changed, 216 insertions(+) create mode 100644 src/ast/ast.ts create mode 100644 src/ast/expr.ts create mode 100644 src/ast/match.ts create mode 100644 src/ast/op.ts create mode 100644 src/ast/statement.ts create mode 100644 src/ast/type-def.ts diff --git a/src/ast/ast.ts b/src/ast/ast.ts new file mode 100644 index 00000000..4072363e --- /dev/null +++ b/src/ast/ast.ts @@ -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 +} + diff --git a/src/ast/expr.ts b/src/ast/expr.ts new file mode 100644 index 00000000..959324e3 --- /dev/null +++ b/src/ast/expr.ts @@ -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 +} diff --git a/src/ast/match.ts b/src/ast/match.ts new file mode 100644 index 00000000..bbfe5b70 --- /dev/null +++ b/src/ast/match.ts @@ -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 +} diff --git a/src/ast/op.ts b/src/ast/op.ts new file mode 100644 index 00000000..8cc32d03 --- /dev/null +++ b/src/ast/op.ts @@ -0,0 +1,4 @@ +export type UnaryOp = 'todo' + +export type BinaryOp = 'todo' + diff --git a/src/ast/statement.ts b/src/ast/statement.ts new file mode 100644 index 00000000..cf6556da --- /dev/null +++ b/src/ast/statement.ts @@ -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[] +} diff --git a/src/ast/type-def.ts b/src/ast/type-def.ts new file mode 100644 index 00000000..fbd97741 --- /dev/null +++ b/src/ast/type-def.ts @@ -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 +} +