From fea8418be63d21ff8f0d6cf6353d1da541464779 Mon Sep 17 00:00:00 2001 From: Mark Stahl Date: Thu, 30 May 2013 15:05:10 -0400 Subject: [PATCH] Worked on #65. Added Arrays to the parser. Evaluation returns nil. --- src/soma/ast/array.go | 38 +++++++++++++++++++++++++++++ src/soma/parse/array.go | 53 +++++++++++++++++++++++++++++++++++++++++ src/soma/parse/expr.go | 3 +++ 3 files changed, 94 insertions(+) create mode 100644 src/soma/ast/array.go create mode 100644 src/soma/parse/array.go diff --git a/src/soma/ast/array.go b/src/soma/ast/array.go new file mode 100644 index 0000000..b408583 --- /dev/null +++ b/src/soma/ast/array.go @@ -0,0 +1,38 @@ +// Copyright (C) 2013 Mark Stahl + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package ast + +import ( + "soma/rt" + "fmt" +) + +type Array struct { + Exprs []rt.Expr +} + +func (a *Array) Eval(s *rt.Scope) rt.Value { + return nil +} + +func (a *Array) Visit(s *rt.Scope) rt.Value { + fmt.Printf("%#v\n", a.Exprs) + return nil +} + +func (a *Array) String() string { + return fmt.Sprintf("%#v", a.Exprs) +} \ No newline at end of file diff --git a/src/soma/parse/array.go b/src/soma/parse/array.go new file mode 100644 index 0000000..9b825ea --- /dev/null +++ b/src/soma/parse/array.go @@ -0,0 +1,53 @@ +// Copyright (C) 2013 Mark Stahl + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package parse + +import ( + "soma/ast" + "soma/rt" + "soma/scan" +) + +// array := +// '[' [expressions] ']' +// +func (p *Parser) parseArray() (a *ast.Array) { + p.expect(scan.LBRACK) + + a = &ast.Array{Exprs: p.parseExpressions([]rt.Expr{})} + + p.expect(scan.RBRACK) + return +} + +// expressions := +// [expr ('.' expressions)*] +// +func (p *Parser) parseExpressions(exprs []rt.Expr) []rt.Expr { + exprs = append(exprs, p.parseExpr()) + + switch p.tok { + case scan.RBRACK: + return exprs + case scan.PERIOD: + p.next() + exprs = p.parseExpressions(exprs) + default: + p.error(p.pos, "expected expression, '.', or ']', found '%s'", p.lit) + p.next() + } + return exprs +} \ No newline at end of file diff --git a/src/soma/parse/expr.go b/src/soma/parse/expr.go index 824f15b..4ab2496 100644 --- a/src/soma/parse/expr.go +++ b/src/soma/parse/expr.go @@ -56,6 +56,8 @@ func (p *Parser) parsePrimary() (recv rt.Expr) { recv = &ast.Global{Value: name} case scan.LBRACE: recv = p.parseBlock() + case scan.LBRACK: + recv = p.parseArray() case scan.LPAREN: recv = p.parseParenExpr() case scan.RETURN: @@ -76,6 +78,7 @@ func (p *Parser) isPrimary() bool { return p.tok == scan.IDENT || p.tok == scan.GLOBAL || p.tok == scan.LBRACE || + p.tok == scan.LBRACK || p.tok == scan.LPAREN || p.tok == scan.INT }