From 4691f6c3e56e4d7228fdecc626d6b9cf04e4abcd Mon Sep 17 00:00:00 2001 From: tijani Date: Mon, 23 May 2022 14:26:34 +0000 Subject: [PATCH] Basic AST to represent expressions, statement, identifiers I now understand how it works. git-svn-id: https://svn.tlawal.org/svn/monkey@12 f6afcba9-9ef1-4bdd-9b72-7484f5705bac --- ast/ast.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 ast/ast.go diff --git a/ast/ast.go b/ast/ast.go new file mode 100644 index 0000000..5c80a03 --- /dev/null +++ b/ast/ast.go @@ -0,0 +1,52 @@ +package ast + +import "monkey/token" + +type Node interface { + TokeLiteral() string +} + +type Statement interface { + Node + statement_node() +} + +type Expression interface { + Node + expression_node() +} + +type Program struct { + Statements []Statement +} + +type Identifier struct { + Token token.Token // the token.IDENT token + Value string +} + +type LetStatement struct { + Token token.Token // the token.LET token + Name *Identifier + Value Expression +} + +func (ls *LetStatement) statement_node() {} + +func (ls *LetStatement) TokenLiteral() string { + return ls.Token.Literal +} + +func (i *Identifier) expression_node() {} + +func (i *Identifier) TokenLiteral() string { + return i.Token.Literal +} + +func (p *Program) TokenLiteral() string { + if len(p.Statements) > 0 { + return p.Statements[0].TokenLiteral() + } else { + return "" + } +}