umx_compiler

UMX virtual machine "Monkey" interpreter / bytecode compiler
git clone git://bsandro.tech/umx_compiler
Log | Files | Refs | README | LICENSE

ast.go (7150B)


      1 package ast
      2 
      3 import (
      4 	"bytes"
      5 	"umx_compiler/token"
      6 	"strings"
      7 )
      8 
      9 type Node interface {
     10 	TokenLiteral() string
     11 	String() string
     12 }
     13 
     14 type Statement interface {
     15 	Node
     16 	statementNode()
     17 }
     18 
     19 type Expression interface {
     20 	Node
     21 	expressionNode()
     22 }
     23 
     24 type Boolean struct {
     25 	Token token.Token
     26 	Value bool
     27 }
     28 
     29 func (b *Boolean) expressionNode() {}
     30 func (b *Boolean) TokenLiteral() string {
     31 	return b.Token.Literal
     32 }
     33 func (b *Boolean) String() string {
     34 	return b.Token.Literal
     35 }
     36 
     37 type PrefixExpression struct {
     38 	Token    token.Token
     39 	Operator string
     40 	Right    Expression
     41 }
     42 
     43 func (pe *PrefixExpression) expressionNode() {}
     44 func (pe *PrefixExpression) TokenLiteral() string {
     45 	return pe.Token.Literal
     46 }
     47 func (pe *PrefixExpression) String() string {
     48 	var out bytes.Buffer
     49 	out.WriteString("(")
     50 	out.WriteString(pe.Operator)
     51 	out.WriteString(pe.Right.String())
     52 	out.WriteString(")")
     53 	return out.String()
     54 }
     55 
     56 type InfixExpression struct {
     57 	Token    token.Token
     58 	Left     Expression
     59 	Operator string
     60 	Right    Expression
     61 }
     62 
     63 func (ie *InfixExpression) expressionNode() {}
     64 func (ie *InfixExpression) TokenLiteral() string {
     65 	return ie.Token.Literal
     66 }
     67 func (ie *InfixExpression) String() string {
     68 	var out bytes.Buffer
     69 	out.WriteString("(")
     70 	out.WriteString(ie.Left.String())
     71 	out.WriteString(" " + ie.Operator + " ")
     72 	out.WriteString(ie.Right.String())
     73 	out.WriteString(")")
     74 	return out.String()
     75 
     76 }
     77 
     78 type Identifier struct {
     79 	Token token.Token
     80 	Value string
     81 }
     82 
     83 func (i *Identifier) expressionNode() {}
     84 func (i *Identifier) TokenLiteral() string {
     85 	return i.Token.Literal
     86 }
     87 func (i *Identifier) String() string {
     88 	return i.Value
     89 }
     90 
     91 type LetStatement struct {
     92 	Token token.Token
     93 	Name  *Identifier
     94 	Value Expression
     95 }
     96 
     97 func (l *LetStatement) statementNode() {}
     98 func (l *LetStatement) TokenLiteral() string {
     99 	return l.Token.Literal
    100 }
    101 func (l *LetStatement) String() string {
    102 	var out bytes.Buffer
    103 	out.WriteString(l.TokenLiteral() + " ")
    104 	out.WriteString(l.Name.String())
    105 	out.WriteString(" = ")
    106 	if l.Value != nil {
    107 		out.WriteString(l.Value.String())
    108 	}
    109 	out.WriteString(";")
    110 	return out.String()
    111 }
    112 
    113 type ReturnStatement struct {
    114 	Token       token.Token
    115 	ReturnValue Expression
    116 }
    117 
    118 func (rs *ReturnStatement) statementNode() {}
    119 func (rs *ReturnStatement) TokenLiteral() string {
    120 	return rs.Token.Literal
    121 }
    122 func (rs *ReturnStatement) String() string {
    123 	var out bytes.Buffer
    124 	out.WriteString(rs.TokenLiteral() + " ")
    125 	if rs.ReturnValue != nil {
    126 		out.WriteString(rs.ReturnValue.String())
    127 	}
    128 	out.WriteString(";")
    129 	return out.String()
    130 }
    131 
    132 type ExpressionStatement struct {
    133 	Token      token.Token
    134 	Expression Expression
    135 }
    136 
    137 func (ec *ExpressionStatement) statementNode() {}
    138 func (ec *ExpressionStatement) TokenLiteral() string {
    139 	return ec.Token.Literal
    140 }
    141 func (ec *ExpressionStatement) String() string {
    142 	if ec.Expression != nil {
    143 		return ec.Expression.String()
    144 	}
    145 	return ""
    146 }
    147 
    148 type Program struct {
    149 	Statements []Statement
    150 }
    151 
    152 func (p *Program) TokenLiteral() string {
    153 	if len(p.Statements) > 0 {
    154 		return p.Statements[0].TokenLiteral()
    155 	} else {
    156 		return ""
    157 	}
    158 }
    159 
    160 func (p *Program) String() string {
    161 	var out bytes.Buffer
    162 	for _, s := range p.Statements {
    163 		out.WriteString(s.String())
    164 	}
    165 	return out.String()
    166 }
    167 
    168 type IntegerLiteral struct {
    169 	Token token.Token
    170 	Value int64
    171 }
    172 
    173 func (il *IntegerLiteral) expressionNode() {}
    174 func (il *IntegerLiteral) TokenLiteral() string {
    175 	return il.Token.Literal
    176 }
    177 func (il *IntegerLiteral) String() string {
    178 	return il.Token.Literal
    179 }
    180 
    181 type IfExpression struct {
    182 	Token       token.Token
    183 	Condition   Expression
    184 	Consequence *BlockStatement
    185 	Alternative *BlockStatement
    186 }
    187 
    188 func (ie *IfExpression) expressionNode() {}
    189 func (ie *IfExpression) TokenLiteral() string {
    190 	return ie.Token.Literal
    191 }
    192 func (ie *IfExpression) String() string {
    193 	var out bytes.Buffer
    194 	out.WriteString("if")
    195 	out.WriteString(ie.Condition.String())
    196 	out.WriteString(" ")
    197 	out.WriteString(ie.Consequence.String())
    198 	if ie.Alternative != nil {
    199 		out.WriteString(" else ")
    200 		out.WriteString(ie.Alternative.String())
    201 	}
    202 	return out.String()
    203 }
    204 
    205 type BlockStatement struct {
    206 	Token      token.Token
    207 	Statements []Statement
    208 }
    209 
    210 func (bs *BlockStatement) expressionNode() {}
    211 func (bs *BlockStatement) TokenLiteral() string {
    212 	return bs.Token.Literal
    213 }
    214 func (bs *BlockStatement) String() string {
    215 	var out bytes.Buffer
    216 	for _, s := range bs.Statements {
    217 		out.WriteString(s.String())
    218 	}
    219 	return out.String()
    220 }
    221 
    222 type FunctionLiteral struct {
    223 	Token      token.Token
    224 	Parameters []*Identifier
    225 	Body       *BlockStatement
    226 }
    227 
    228 func (fl *FunctionLiteral) expressionNode() {}
    229 func (fl *FunctionLiteral) TokenLiteral() string {
    230 	return fl.Token.Literal
    231 }
    232 func (fl *FunctionLiteral) String() string {
    233 	var out bytes.Buffer
    234 	params := []string{}
    235 	for _, p := range fl.Parameters {
    236 		params = append(params, p.String())
    237 	}
    238 	out.WriteString(fl.TokenLiteral())
    239 	out.WriteString("(")
    240 	out.WriteString(strings.Join(params, ", "))
    241 	out.WriteString(") ")
    242 	out.WriteString(fl.Body.String())
    243 	return out.String()
    244 }
    245 
    246 type CallExpression struct {
    247 	Token     token.Token
    248 	Function  Expression
    249 	Arguments []Expression
    250 }
    251 
    252 func (ce *CallExpression) expressionNode() {}
    253 func (ce *CallExpression) TokenLiteral() string {
    254 	return ce.Token.Literal
    255 }
    256 func (ce *CallExpression) String() string {
    257 	var out bytes.Buffer
    258 	args := []string{}
    259 	for _, a := range ce.Arguments {
    260 		args = append(args, a.String())
    261 	}
    262 	out.WriteString(ce.Function.String())
    263 	out.WriteString("(")
    264 	out.WriteString(strings.Join(args, ", "))
    265 	out.WriteString(")")
    266 	return out.String()
    267 }
    268 
    269 type StringLiteral struct {
    270 	Token token.Token
    271 	Value string
    272 }
    273 
    274 func (sl *StringLiteral) expressionNode() {}
    275 func (sl *StringLiteral) TokenLiteral() string {
    276 	return sl.Token.Literal
    277 }
    278 func (sl *StringLiteral) String() string {
    279 	return sl.Token.Literal
    280 }
    281 
    282 type ArrayLiteral struct {
    283 	Token    token.Token
    284 	Elements []Expression
    285 }
    286 
    287 func (al *ArrayLiteral) expressionNode() {}
    288 func (al *ArrayLiteral) TokenLiteral() string {
    289 	return al.Token.Literal
    290 }
    291 func (al *ArrayLiteral) String() string {
    292 	var out bytes.Buffer
    293 	elements := []string{}
    294 	for _, el := range al.Elements {
    295 		elements = append(elements, el.String())
    296 	}
    297 	out.WriteString("[")
    298 	out.WriteString(strings.Join(elements, ", "))
    299 	out.WriteString("]")
    300 	return out.String()
    301 }
    302 
    303 type IndexExpression struct {
    304 	Token token.Token
    305 	Left  Expression
    306 	Index Expression
    307 }
    308 
    309 func (ie *IndexExpression) expressionNode() {}
    310 func (ie *IndexExpression) TokenLiteral() string {
    311 	return ie.Token.Literal
    312 }
    313 func (ie *IndexExpression) String() string {
    314 	var out bytes.Buffer
    315 	out.WriteString("(")
    316 	out.WriteString(ie.Left.String())
    317 	out.WriteString("[")
    318 	out.WriteString(ie.Index.String())
    319 	out.WriteString("])")
    320 	return out.String()
    321 }
    322 
    323 type HashLiteral struct {
    324 	Token token.Token
    325 	Pairs map[Expression]Expression
    326 }
    327 
    328 func (hl *HashLiteral) expressionNode() {}
    329 func (hl *HashLiteral) TokenLiteral() string {
    330 	return hl.Token.Literal
    331 }
    332 func (hl *HashLiteral) String() string {
    333 	var out bytes.Buffer
    334 	pairs := []string{}
    335 	for k, v := range hl.Pairs {
    336 		pairs = append(pairs, k.String()+":"+v.String())
    337 	}
    338 	out.WriteString("{")
    339 	out.WriteString(strings.Join(pairs, ", "))
    340 	out.WriteString("}")
    341 	return out.String()
    342 }