Skip to content

Commit

Permalink
Implemented keywords; fixed bugs in step1.
Browse files Browse the repository at this point in the history
  • Loading branch information
h3rald committed Oct 11, 2015
1 parent a774d26 commit 8c50295
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
3 changes: 3 additions & 0 deletions nim2/printer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ proc prStr*(p: Printer, form: Node, printReadably = true): string =
result &= ")"
of nInt:
result = $form.intVal
of nKeyword:
result = ":" & form.keyVal
of nString:
if printReadably:
result = form.stringVal.replace("\n", "\\n").replace("\"", "\\\"")
else:
result = form.stringVal
result = "\"$1\"" % [result]
of nSymbol:
result = form.symbolVal
of nAtom:
Expand Down
33 changes: 23 additions & 10 deletions nim2/reader.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ let
REGEX_INT = re"""^[\d]+$"""
REGEX_SYMBOL = re"""^[\w]+$"""
REGEX_STRING = re"""^".*"$"""
REGEX_KEYWORD = re"""^:[\w]+$"""


const
UNMATCHED_PAREN = "expected ')', got EOF"
UNMATCHED_DOUBLE_QUOTE = "expected '\"', got EOF"

var
DEBUG = false

template dbg(x: stmt) =
if DEBUG:
x

proc error(str: string) =
stderr.write str
stderr.write "\n"
Expand All @@ -26,17 +34,20 @@ proc tokenizer(str: string): seq[string] =
s = str
token: string
while s != "" and s.match(REGEX_TOKEN, matches) and matches[0] != nil and matches[0] != "":
#echo "--- matches ---"
#for m in matches:
# echo "->", m, "<-"
#echo "---------------"
dbg:
echo "--- matches ---"
for m in matches:
echo "->", m, "<-"
echo "---------------"
token = matches[0]
#echo "Token: ->", token, "<-"
dbg:
echo "Token: ->", token, "<-"
result.add(token)
#echo s.find(token)
#echo token.len
dbg:
echo s.find(token)
echo token.len
s = s.substr(s.find(token) + token.len, s.len-1)
#echo "->", s, "<-"
dbg: echo "->", s, "<-"
matches[0] = nil
if token.len == 0:
error UNMATCHED_DOUBLE_QUOTE
Expand All @@ -54,7 +65,10 @@ proc next*(r: var Reader): string =

proc readAtom*(r: var Reader): Node =
let token = r.peek()
if token.match(REGEX_STRING):
if token.match(REGEX_KEYWORD):
result.kind = nKeyword
result.keyVal = token.substr(1, token.len-1)
elif token.match(REGEX_STRING):
result.kind = nString
result.stringVal = token.substr(1, token.len-2).replace("\\\"", "\"").replace("\\n", "\n")
elif token.match(REGEX_INT):
Expand Down Expand Up @@ -114,4 +128,3 @@ proc readForm*(r: var Reader): Node =
result = newNlist(@[newNsymbol("splice-unquote"), r.readForm()])
else:
result = r.readAtom()

4 changes: 3 additions & 1 deletion nim2/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ type
nAtom,
nSymbol,
nString,
nInt
nInt,
nKeyword
Node* = object
case kind*: NodeKind
of nList: listVal*: seq[Node]
of nSymbol: symbolVal*: string
of nString: stringVal*: string
of nInt: intVal*: int
of nAtom: atomVal*: string
of nKeyword: keyVal*: string
ParsingError* = object of Exception

proc `position=`*(r: var Reader, value: int) {.inline.} =
Expand Down

0 comments on commit 8c50295

Please sign in to comment.