toylang
Version:
A toy programming language built with TypeScript for learning purposes
174 lines (173 loc) • 6.25 kB
TypeScript
export declare namespace tl {
enum SyntaxKind {
Program = "Program",
Identifier = "Identifier",
StringLiteral = "StringLiteral",
NumericLiteral = "NumericLiteral",
NullLiteral = "NullLiteral",
BooleanLiteral = "BooleanLiteral",
ClassDeclaration = "ClassDeclaration",
FunctionDeclaration = "FunctionDeclaration",
VariableDeclaration = "VariableDeclaration",
VariableStatement = "VariableStatement",
Super = "Super",
NewExpression = "NewExpression",
ThisExpression = "ThisExpression",
UnaryExpression = "UnaryExpression",
MemberExpression = "MemberExpression",
CallExpression = "CallExpression",
AssignmentExpression = "AssignmentExpression",
BinaryExpression = "BinaryExpression",
LogicalExpression = "LogicalExpression",
ExpressionStatement = "ExpressionStatement",
ReturnStatement = "ReturnStatement",
EmptyStatement = "EmptyStatement",
BlockStatement = "BlockStatement",
IfStatement = "IfStatement",
ForStatement = "ForStatement",
WhileStatement = "WhileStatement",
DoWhileStatement = "DoWhileStatement"
}
interface Program {
type: SyntaxKind.Program;
body: Statement[];
}
interface Identifier {
type: SyntaxKind.Identifier;
name: string;
}
interface StringLiteral {
type: SyntaxKind.StringLiteral;
value: string;
}
interface NumericLiteral {
type: SyntaxKind.NumericLiteral;
value: number;
}
interface NullLiteral {
type: SyntaxKind.NullLiteral;
value: null;
}
interface BooleanLiteral {
type: SyntaxKind.BooleanLiteral;
value: boolean;
}
type Literal = StringLiteral | NumericLiteral | BooleanLiteral | NullLiteral;
interface ClassDeclaration {
type: SyntaxKind.ClassDeclaration;
superClass: Identifier | null;
id: Identifier;
body: BlockStatement;
}
interface FunctionDeclaration {
type: SyntaxKind.FunctionDeclaration;
name: Identifier;
body: BlockStatement;
params: Identifier[] | null;
}
interface VariableDeclaration {
type: SyntaxKind.VariableDeclaration;
id: Identifier;
init: Literal | Expression | null;
}
interface VariableStatement {
type: SyntaxKind.VariableStatement;
declarations: VariableDeclaration[];
}
interface Super {
type: SyntaxKind.Super;
}
interface NewExpression {
type: SyntaxKind.NewExpression;
callee: MemberExpression;
arguments: Arguments;
}
interface ThisExpression {
type: SyntaxKind.ThisExpression;
}
type UnaryOperators = "+" | "-";
interface UnaryExpression {
type: SyntaxKind.UnaryExpression;
operator: string;
argument: CallExpression | CallMemberExpression | UnaryExpression;
}
interface MemberExpression {
type: SyntaxKind.MemberExpression;
computed?: boolean;
object: any;
property: Identifier | Expression;
}
type CallMemberExpression = MemberExpression | CallExpression;
interface CallExpression {
type: SyntaxKind.CallExpression;
callee: MemberExpression;
arguments: Arguments;
}
type Arguments = (CallExpression | AssignmentExpression | MemberExpression | LogicalExpression)[];
type AssignmentOperators = "=" | "+=" | "-=" | "/=" | "*=";
interface AssignmentExpression {
type: SyntaxKind.AssignmentExpression;
operator: AssignmentOperators;
left: Identifier | LogicalExpression | BinaryExpression | Expression;
right: Identifier | LogicalExpression | BinaryExpression | Expression;
}
interface BinaryExpression {
type: SyntaxKind.BinaryExpression;
operator: string;
left: LogicalExpression | BinaryExpression | Identifier | Literal | Expression;
right: LogicalExpression | BinaryExpression | Identifier | Literal | Expression;
}
interface LogicalExpression {
type: SyntaxKind.LogicalExpression;
operator: string;
left: LogicalExpression | BinaryExpression | Identifier | Literal | Expression;
right: LogicalExpression | BinaryExpression | Identifier | Literal | Expression;
}
type Expression = CallExpression | AssignmentExpression | MemberExpression | LogicalExpression;
interface ExpressionStatement {
type: SyntaxKind.ExpressionStatement;
expression: Expression;
}
interface ReturnStatement {
type: SyntaxKind.ReturnStatement;
argument: Expression | null;
}
interface EmptyStatement {
type: SyntaxKind.EmptyStatement;
}
interface BlockStatement {
type: SyntaxKind.BlockStatement;
body: Statement[];
}
interface FunctionStatement {
type: SyntaxKind.FunctionDeclaration;
name: Identifier;
body: BlockStatement;
params: Identifier[] | null;
}
interface IfStatement {
type: SyntaxKind.IfStatement;
test: Literal | Identifier | Expression;
consequent: Statement;
alternate: Statement | null;
}
interface ForStatement {
type: SyntaxKind.ForStatement;
test: Expression | null;
init: VariableStatement | Expression | null;
update: Expression | null;
body: Statement;
}
interface WhileStatement {
type: SyntaxKind.WhileStatement;
test: Expression;
body: Statement;
}
interface DoWhileStatement {
type: SyntaxKind.DoWhileStatement;
test: Expression;
body: Statement;
}
type IterationStatement = ForStatement | WhileStatement | DoWhileStatement;
type Statement = VariableStatement | IfStatement | IterationStatement | FunctionStatement | ReturnStatement | ClassDeclaration | EmptyStatement | BlockStatement | ExpressionStatement;
}