Skip to content

Commit

Permalink
fixing issue with ParseProgram returning multiple values
Browse files Browse the repository at this point in the history
  • Loading branch information
dfirebaugh committed Jan 5, 2025
1 parent 10cc9ce commit a1a282d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
7 changes: 6 additions & 1 deletion compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/dfirebaugh/punch/emitters/wat"
"github.com/dfirebaugh/punch/lexer"
"github.com/dfirebaugh/punch/parser"
"github.com/sirupsen/logrus"
)

const (
Expand All @@ -15,7 +16,11 @@ const (
func Compile(filename string, source string) (string, []byte, string) {
l := lexer.New(filename, source)
p := parser.New(l)
program := p.ParseProgram(filename)
program, err := p.ParseProgram(filename)
if err != nil {
logrus.Error(err)
return "", nil, ""
}
var ast string
wat := wat.GenerateWAT(program, true)
if !astDisabled {
Expand Down
22 changes: 21 additions & 1 deletion examples/struct.pun
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
pkg main

import (

)

struct extra {
str note
}
Expand Down Expand Up @@ -39,5 +43,21 @@ fn send_message() {
println(msg.other.extra.note)
}

send_message()

message get_message() {
message msg = message {
sender: 2,
receiver: 4,
body: "hello, world",
other: other {
message: "hello",
extra: extra {
note: "this is extra info",
},
},
}
return msg
}

message msg = get_message()
println(msg)
2 changes: 1 addition & 1 deletion parser/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (p *Parser) parseIfStatement() (*ast.IfStatement, error) {
}

if !p.expectCurrentTokenIs(token.LBRACE) {
return nil, p.errorf("expecting current token is a '{'", p.curToken.Literal, p.peekToken.Literal)
return nil, p.error("expecting current token is a '{'", p.curToken.Literal, p.peekToken.Literal)
}
stmt.Consequence, err = p.parseBlockStatement()
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ func (repl *REPL) handleLine(line string) bool {
fmt.Fprintf(repl.out, "Command entered: %s\n", line)
l := lexer.New("repl", line)
p := parser.New(l)
program := p.ParseProgram("repl")
program, err := p.ParseProgram("repl")
if err != nil {
println(err.Error())
return true
}

println("ast:")
json, err := program.JSONPretty()
Expand Down

0 comments on commit a1a282d

Please sign in to comment.