Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Worked on #65. Added Arrays to the parser. Evaluation returns nil.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjstahl committed May 30, 2013
1 parent 568b9ca commit fea8418
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/soma/ast/array.go
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

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)
}
53 changes: 53 additions & 0 deletions src/soma/parse/array.go
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

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
}
3 changes: 3 additions & 0 deletions src/soma/parse/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
}
Expand Down

0 comments on commit fea8418

Please sign in to comment.