Skip to content

Commit

Permalink
string literal!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonak-Adipta-Kalita committed Apr 8, 2023
1 parent 31f9a7d commit 728415b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
9 changes: 9 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,12 @@ func (ce *CallExpression) String() string {
out.WriteString(")")
return out.String()
}

type StringLiteral struct {
Token token.Token
Value string
}

func (sl *StringLiteral) expressionNode() {}
func (sl *StringLiteral) TokenLiteral() string { return sl.Token.Literal }
func (sl *StringLiteral) String() string { return sl.Token.Literal }
2 changes: 2 additions & 0 deletions evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func Eval(node ast.Node, env *object.Environment) object.Object {
return args[0]
}
return applyFunction(function, args)
case *ast.StringLiteral:
return &object.String{Value: node.Value}
}
return nil
}
Expand Down
14 changes: 14 additions & 0 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func (l *Lexer) NextToken() token.Token {
tok = newToken(token.LBRACE, l.ch)
case '}':
tok = newToken(token.RBRACE, l.ch)
case '"':
tok.Type = token.STRING
tok.Literal = l.readString()
case 0:
tok.Literal = ""
tok.Type = token.EOF
Expand Down Expand Up @@ -137,6 +140,17 @@ func (l *Lexer) readNumber() string {
return l.input[position:l.position]
}

func (l *Lexer) readString() string {
position := l.position + 1
for {
l.readChar()
if l.ch == '"' || l.ch == 0 {
break
}
}
return l.input[position:l.position]
}

func isDigit(ch byte) bool {
return '0' <= ch && ch <= '9'
}
Expand Down
18 changes: 14 additions & 4 deletions object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ type Object interface {
}

const (
INTEGER_OBJ = "INTEGER"
BOOLEAN_OBJ = "BOOLEAN"
NULL_OBJ = "NULL"
INTEGER_OBJ = "INTEGER"
STRING_OBJ = "STRING"
BOOLEAN_OBJ = "BOOLEAN"

NULL_OBJ = "NULL"
ERROR_OBJ = "ERROR"

RETURN_VALUE_OBJ = "RETURN_VALUE"
ERROR_OBJ = "ERROR"
FUNCTION_OBJ = "FUNCTION"
)

Expand Down Expand Up @@ -78,3 +81,10 @@ type Error struct {

func (e *Error) Type() ObjectType { return ERROR_OBJ }
func (e *Error) Inspect() string { return "ERROR: " + e.Message }

type String struct {
Value string
}

func (s *String) Type() ObjectType { return STRING_OBJ }
func (s *String) Inspect() string { return s.Value }
14 changes: 8 additions & 6 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func New(l *lexer.Lexer) *Parser {
p.prefixParseFns = make(map[token.TokenType]prefixParseFn)
p.registerPrefix(token.IDENTIFIER, p.parseIdentifier)
p.registerPrefix(token.INT, p.parseIntegerLiteral)
p.registerPrefix(token.STRING, p.parseStringLiteral)

p.registerPrefix(token.BANG, p.parsePrefixExpression)
p.registerPrefix(token.MINUS, p.parsePrefixExpression)
p.registerPrefix(token.TRUE, p.parseBoolean)
Expand Down Expand Up @@ -104,6 +106,10 @@ func (p *Parser) curPrecedence() int {
return LOWEST
}

func (p *Parser) parseStringLiteral() ast.Expression {
return &ast.StringLiteral{Token: p.curToken, Value: p.curToken.Literal}
}

func (p *Parser) parseCallExpression(function ast.Expression) ast.Expression {
exp := &ast.CallExpression{Token: p.curToken, Function: function}
exp.Arguments = p.parseCallArguments()
Expand Down Expand Up @@ -202,9 +208,7 @@ func (p *Parser) parseBlockStatement() *ast.BlockStatement {
p.nextToken()
for !p.curTokenIs(token.RBRACE) && !p.curTokenIs(token.EOF) {
stmt := p.parseStatement()
if stmt != nil {
block.Statements = append(block.Statements, stmt)
}
block.Statements = append(block.Statements, stmt)
p.nextToken()
}
return block
Expand Down Expand Up @@ -290,9 +294,7 @@ func (p *Parser) ParseProgram() *ast.Program {

for p.curToken.Type != token.EOF {
stmt := p.parseStatement()
if stmt != nil {
program.Statements = append(program.Statements, stmt)
}
program.Statements = append(program.Statements, stmt)
p.nextToken()
}

Expand Down

0 comments on commit 728415b

Please sign in to comment.