Skip to content

Commit

Permalink
parse function with arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Aug 17, 2024
1 parent bd03f1f commit e033865
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
12 changes: 10 additions & 2 deletions davi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ func main() {
fmt.Println("Running davi.go")

input := []byte(`
// This is a comment
$firstMessage = "Hello World";
$secondMessage = "Hello Davi";
$secondMessage = "Hello DavinciScript";
function person($name, $age) {
}
echo "$firstMessage";
echo ($secondMessage);
`)

Expand Down
18 changes: 14 additions & 4 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parser
import (
"fmt"
. "github.com/DavinciScript/Davi/lexer"
"github.com/hokaccha/go-prettyjson"
"strconv"
)

Expand Down Expand Up @@ -194,6 +195,7 @@ func (p *parser) params() ([]string, bool) {
p.error("expected , between parameters")
}
param := p.val
p.expect(DOLAR)
p.expect(NAME)
params = append(params, param)
if p.tok == ELLIPSIS {
Expand Down Expand Up @@ -315,6 +317,7 @@ func (p *parser) call() Expression {
p.error("can only have ... after last argument")
}
p.expect(RPAREN)
p.expect(SEMI)
expr = &Call{pos, expr, args, gotEllipsis}
} else if p.tok == LBRACKET {
pos := p.pos
Expand All @@ -339,6 +342,12 @@ func (p *parser) call() Expression {
// LPAREN expression RPAREN
func (p *parser) primary() Expression {
switch p.tok {
case NAME:
name := p.val
pos := p.pos
p.next()

return &Variable{pos, name}
case DOLAR:
p.expect(DOLAR)
name := p.val
Expand Down Expand Up @@ -387,12 +396,13 @@ func (p *parser) primary() Expression {
p.next()
expr := p.expression()
p.expect(RPAREN)
p.expect(SEMI)
return expr
default:
//formatter := prettyjson.NewFormatter()
//output, _ := formatter.Marshal(p.tok)
//fmt.Println(string(output))
p.error("expected expression, not %s", p.tok)
formatter := prettyjson.NewFormatter()
output, _ := formatter.Marshal(p.val)
fmt.Println(string(output))
p.error("expected expression, not %s val %s", p.tok, p.val)
return nil
}
}
Expand Down

0 comments on commit e033865

Please sign in to comment.