Fixed parseString method

This commit is contained in:
nocturn9x 2020-08-05 17:50:29 +02:00
parent 0e6f4556db
commit 9eb4b421cf
6 changed files with 25 additions and 10 deletions

Binary file not shown.

View File

@ -2,6 +2,7 @@ import tables
import meta/tokentype
import meta/tokenobject
import meta/exceptions
import meta/valueobject
import strformat
import system
@ -36,7 +37,7 @@ type Lexer* = object
current: int
func initLexer*(source: string): Lexer =
proc initLexer*(source: string): Lexer =
result = Lexer(source: source, tokens: @[], line: 1, start: 0, current: 0)
@ -63,10 +64,11 @@ proc peekNext*(self: Lexer): string =
result = &"{self.source[self.current + 1]}"
proc createToken*[T](self: var Lexer, tokenType: TokenType, literal: T): Token =
proc createToken*(self: var Lexer, tokenType: TokenType, literal: Value): Token =
result = Token(kind: tokenType,
lexeme: self.source[self.start..self.current],
literal: literal
lexeme: self.source[self.start..<self.current],
literal: literal,
line: self.line
)
@ -78,8 +80,9 @@ proc parseString*(self: var Lexer, delimiter: string) =
if self.done():
raise newException(ParseError, &"Unterminated string literal at {self.line}")
discard self.step()
let value = self.source[self.start + 1..self.current - 1] # Get the value between quotes
let token = self.createToken[string](STR, value)
let value = StrValue(value: self.source[self.start..<self.current - 1]) # Get the value between quotes
let token = self.createToken(STR, value)
self.tokens.add(token)
var lexer = initLexer("print")
echo lexer.createToken[string](STR, "ehlo", STR)
var lexer = initLexer("'hello'")

Binary file not shown.

View File

@ -1,9 +1,11 @@
import tokentype
import valueobject
# Token object
type
Token*[T] = ref object of RootOBJ
Token* = ref object
kind*: TokenType
lexeme*: string
literal*: T
literal*: Value
line*: int

Binary file not shown.

View File

@ -0,0 +1,10 @@
# Value objects
type
Value* = ref object of RootObj
StrValue* = ref object of Value
value*: string
IntValue* = ref object of Value
value*: int
FloatValue* = ref object of Value
value*: float