Skip to content

Commit

Permalink
Added parse tracer
Browse files Browse the repository at this point in the history
- Added a file tracer.go for functions that handle code tracing for the parser
- Added statements in the parsers to view the trace if the global traceOn value is set
  • Loading branch information
manishmeganathan committed Jun 13, 2021
1 parent 1ee88f6 commit d599d20
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
27 changes: 27 additions & 0 deletions parser/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var precedences = map[lexer.TokenType]int{
lexer.ASTERISK: PRODUCT,
}

var traceON = false

// A function that returns the precedence value
// for the given lexilogical token type
func GetPrecedence(tokentype lexer.TokenType) int {
Expand Down Expand Up @@ -142,6 +144,11 @@ func (p *Parser) parseReturnStatement() *syntaxtree.ReturnStatement {
// A method of Parser that parses the token in the parse
// cursor into an expression statement node for the syntax tree
func (p *Parser) parseExpressionStatement() *syntaxtree.ExpressionStatement {
if traceON {
// Print parser trace
defer untrace(trace("parseExpressionStatement"))
}

// Create an expression statement node with the token
stmt := &syntaxtree.ExpressionStatement{Token: p.cursorToken}
// Parse the full expression
Expand All @@ -160,6 +167,11 @@ func (p *Parser) parseExpressionStatement() *syntaxtree.ExpressionStatement {

// A method of Parser that parses a full expression given a precedence value
func (p *Parser) parseExpression(precedence int) syntaxtree.Expression {
if traceON {
// Print parser trace
defer untrace(trace("parseExpression"))
}

// Retrive the prefix parser function
prefix := p.prefixParseFns[p.cursorToken.Type]
// Check if the prefix parser is null
Expand Down Expand Up @@ -200,6 +212,11 @@ func (p *Parser) parseIdentifier() syntaxtree.Expression {

// A method of Parser that parses an Integer literal
func (p *Parser) parseIntegerLiteral() syntaxtree.Expression {
if traceON {
// Print parser trace
defer untrace(trace("parseIntegerLiteral"))
}

// Create an integer literal node with the token
lit := &syntaxtree.IntegerLiteral{Token: p.cursorToken}

Expand All @@ -223,6 +240,11 @@ func (p *Parser) parseIntegerLiteral() syntaxtree.Expression {

// A method of Parser that parses a Prefix Expression
func (p *Parser) parsePrefixExpression() syntaxtree.Expression {
if traceON {
// Print parser trace
defer untrace(trace("parsePrefixExpression"))
}

// Create a prefix expression node with the token and operator literal
expression := &syntaxtree.PrefixExpression{
Token: p.cursorToken,
Expand All @@ -239,6 +261,11 @@ func (p *Parser) parsePrefixExpression() syntaxtree.Expression {

// A method of Parser that parses an Infix Expression
func (p *Parser) parseInfixExpression(left syntaxtree.Expression) syntaxtree.Expression {
if traceON {
// Print parser trace
defer untrace(trace("parseInfixExpression"))
}

// Create an infix expression node with the token, operator literal and left expression
expression := &syntaxtree.InfixExpression{
Token: p.cursorToken,
Expand Down
32 changes: 32 additions & 0 deletions parser/tracer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package parser

import (
"fmt"
"strings"
)

var traceLevel int = 0

const traceIdentPlaceholder string = "\t"

func identLevel() string {
return strings.Repeat(traceIdentPlaceholder, traceLevel-1)
}

func tracePrint(fs string) {
fmt.Printf("%s%s\n", identLevel(), fs)
}

func incIdent() { traceLevel = traceLevel + 1 }
func decIdent() { traceLevel = traceLevel - 1 }

func trace(msg string) string {
incIdent()
tracePrint("BEGIN " + msg)
return msg
}

func untrace(msg string) {
tracePrint("END " + msg)
decIdent()
}

0 comments on commit d599d20

Please sign in to comment.