Skip to content

Commit

Permalink
feat(parser): first binary expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
xNaCly committed May 8, 2023
1 parent 4b61678 commit e894cf6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
33 changes: 33 additions & 0 deletions consts/expressions.go
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
package consts

type Expression interface {
Eval() float64
}
type PlusExpression struct {
Lhs Expression
Rhs Expression
}
type MinusExpression struct {
Lhs Expression
Rhs Expression
}
type MultiplicationExpression struct {
Lhs Expression
Rhs Expression
}
type DivisionExpression struct {
Lhs Expression
Rhs Expression
}

func (e PlusExpression) Eval() float64 {
return e.Lhs.Eval() + e.Rhs.Eval()
}
func (e MinusExpression) Eval() float64 {
return e.Lhs.Eval() - e.Rhs.Eval()
}
func (e MultiplicationExpression) Eval() float64 {
return e.Lhs.Eval() * e.Rhs.Eval()
}
func (e DivisionExpression) Eval() float64 {
return e.Lhs.Eval() / e.Rhs.Eval()
}
1 change: 0 additions & 1 deletion lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ func (s *Scanner) Start() []consts.Token {
token = append(token, s.buildToken(res, v, v, pos))
} else {
token = append(token, s.buildToken(consts.IDENTIFIER, v, v, pos))
// s.error(v, "unknown identifier, view https://github.com/xnacly/emmy for the complete reference")
}
continue
} else {
Expand Down
30 changes: 29 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,32 @@ func (p *Parser) NewInput(in []consts.Token) *Parser {
return p
}

func (p *Parser) parse() {}
func (p *Parser) Parse() []consts.Expression {
stmts := make([]consts.Expression, 0)
for !p.isAtEnd() {
stmts = append(stmts, p.statment())
}
return stmts
}

func (p *Parser) statment() consts.Expression {
return nil
}

func (p *Parser) peek() consts.Token {
return p.input[p.pos]
}

func (p *Parser) prev() consts.Token {
return p.input[p.pos-1]
}

func (p *Parser) advance() {
if !p.isAtEnd() {
p.pos++
}
}

func (p *Parser) isAtEnd() bool {
return p.pos >= len(p.input)
}

0 comments on commit e894cf6

Please sign in to comment.