Skip to content

Commit

Permalink
feat: part 6
Browse files Browse the repository at this point in the history
  • Loading branch information
florianfmmartin committed Dec 14, 2021
1 parent 0400b18 commit 321d92b
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 85 deletions.
Binary file modified nirth
Binary file not shown.
185 changes: 100 additions & 85 deletions src/nirth.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,88 +23,107 @@ proc endCompile(vm: var VM) =
vm.compiledWords &= " ;;;; "
vm.compileFlag = false

proc runWord(vm: VM, word: string) =
let
formattedWord = ":::: " & word & " "
startIndex = vm.compiledWords.find(formattedWord)
endIndex = vm.compiledWords.find(";;;;", start=startIndex)
wordDefinition = vm.compiledWords[startIndex..<endIndex]
wordDefinitionList = wordDefinition.splitWhitespace()
definitionList = wordDefinitionList[2..^1]
echo(definitionList)

proc processNonCompilingInput(vm: var VM, input: string): bool =
case input:
of "show":
echo(vm.stack)
of "bye":
ctrlc()
of "+": # (b a -- [b+a])
let
a = vm.stack.pop()
b = vm.stack.pop()
vm.stack.add(b + a)
of "-": # (b a -- [b-a])
let
a = vm.stack.pop()
b = vm.stack.pop()
vm.stack.add(b - a)
of "*": # (b a -- [b*a])
let
a = vm.stack.pop()
b = vm.stack.pop()
vm.stack.add(b * a)
of "/": # (b a -- [b/a])
let
a = vm.stack.pop()
b = vm.stack.pop()
vm.stack.add(b div a)
of "%": # (b a -- [b%a])
let
a = vm.stack.pop()
b = vm.stack.pop()
vm.stack.add(b mod a)
of "pop": # (b a -- b)
let a = vm.stack.pop()
echo(a)
of ":":
let _ = 0
vm.startCompile()
of "compiler":
echo(vm.compiledWords)
of ";":
echo("Not in compile mode...")
proc processArrayOfWords(vm: var VM, input_array: seq[string], ok: var bool, silent: bool = false) =

#### processInput
proc processInput(vm: var VM, input: string): bool =

#### processNonCompilingInput
proc processNonCompilingInput(vm: var VM, input: string): bool =

#### runWord
proc runWord(vm: var VM, word: string): bool =
var ok = true
let
formattedWord = ":::: " & word & " "
startIndex = vm.compiledWords.find(formattedWord)
endIndex = vm.compiledWords.find(";;;;", start=startIndex)
wordDefinition = vm.compiledWords[startIndex..<endIndex]
wordDefinitionList = wordDefinition.splitWhitespace()
definitionList = wordDefinitionList[2..^1]
vm.processArrayOfWords(definitionList, ok, silent=true)
if (not ok):
echo("Error caused by `" & word & "`")
return ok
#### runWord

case input:
of "show":
echo(vm.stack)
of "bye":
ctrlc()
of "+": # (b a -- [b+a])
let
a = vm.stack.pop()
b = vm.stack.pop()
vm.stack.add(b + a)
of "-": # (b a -- [b-a])
let
a = vm.stack.pop()
b = vm.stack.pop()
vm.stack.add(b - a)
of "*": # (b a -- [b*a])
let
a = vm.stack.pop()
b = vm.stack.pop()
vm.stack.add(b * a)
of "/": # (b a -- [b/a])
let
a = vm.stack.pop()
b = vm.stack.pop()
vm.stack.add(b div a)
of "%": # (b a -- [b%a])
let
a = vm.stack.pop()
b = vm.stack.pop()
vm.stack.add(b mod a)
of "pop": # (b a -- b)
let a = vm.stack.pop()
echo(a)
of ":":
let _ = 0
vm.startCompile()
of "compiler":
echo(vm.compiledWords)
of ";":
echo("Not in compile mode...")
else:
if (vm.compiledWords.contains((":::: " & input & " "))):
# echo("`" & input & "` exists in dictionary...")
return vm.runWord(input)
else:
echo("I don't know what `" & input & "` means...")
return false
return true
#### processNonCompilingInput

if vm.compileFlag:
case input:
of "compiler":
echo(vm.compiledWords)
of ";":
let _ = 0
vm.endCompile()
else:
let _ = 0
vm.compile(input)
else:
if (vm.compiledWords.contains((":::: " & input & " "))):
# echo("`" & input & "` exists in dictionary...")
vm.runWord(input)
else:
echo("I don't know what `" & input & "` means...")
return false
return true

proc processInput(vm: var VM, input: string): bool =
if vm.compileFlag:
case input:
of "compiler":
echo(vm.compiledWords)
of ";":
let _ = 0
vm.endCompile()
else:
let _ = 0
vm.compile(input)
else:
try:
let integer = parseInt(input)
vm.stack.add(integer)
except ValueError:
try:
return vm.processNonCompilingInput(input)
except IndexDefect:
echo("Stack underflow by `" & input & "` ...")
return false
return true
let integer = parseInt(input)
vm.stack.add(integer)
except ValueError:
try:
return vm.processNonCompilingInput(input)
except IndexDefect:
echo("Stack underflow by `" & input & "` ...")
return false
return true
#### processInput

for i in input_array:
ok = ok and vm.processInput(i)
if ok and (not silent):
echo("ok.")

proc main() {.raises: [].} =
var
Expand All @@ -125,11 +144,7 @@ proc main() {.raises: [].} =

input_array = input.splitWhitespace()

for i in input_array:
ok = ok and vm.processInput(i)

if ok:
echo("ok.")
vm.processArrayOfWords(input_array, ok)

when isMainModule:
main()

0 comments on commit 321d92b

Please sign in to comment.