Add basic string support
This commit is contained in:
parent
9b90f3fa33
commit
680626f125
26
rforth.py
26
rforth.py
|
@ -1,9 +1,10 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
import argparse
|
import argparse
|
||||||
stack = []
|
stack = []
|
||||||
heap = [0] * 20
|
heap = [0] * 100
|
||||||
next_heap = 0
|
next_heap = 0
|
||||||
compiling = False
|
compiling = False
|
||||||
|
reading_string = False
|
||||||
words = {}
|
words = {}
|
||||||
word_name = ""
|
word_name = ""
|
||||||
|
|
||||||
|
@ -19,7 +20,7 @@ def get_input():
|
||||||
def parse_input(input_string, say_ok=True):
|
def parse_input(input_string, say_ok=True):
|
||||||
while len(input_string) > 0:
|
while len(input_string) > 0:
|
||||||
string = str(input_string[0])
|
string = str(input_string[0])
|
||||||
if not compiling:
|
if not compiling and not reading_string:
|
||||||
if string.isnumeric():
|
if string.isnumeric():
|
||||||
add_to_stack(string)
|
add_to_stack(string)
|
||||||
elif string == "stack":
|
elif string == "stack":
|
||||||
|
@ -66,7 +67,14 @@ def parse_input(input_string, say_ok=True):
|
||||||
dump_heap()
|
dump_heap()
|
||||||
elif string == "alloc":
|
elif string == "alloc":
|
||||||
alloc()
|
alloc()
|
||||||
else:
|
elif string == 's"':
|
||||||
|
start_reading_string()
|
||||||
|
elif reading_string:
|
||||||
|
if string == '"':
|
||||||
|
stop_reading_string()
|
||||||
|
else:
|
||||||
|
add_new_word_to_string(string)
|
||||||
|
elif compiling and not reading_string:
|
||||||
if string == ";":
|
if string == ";":
|
||||||
stop_compiling()
|
stop_compiling()
|
||||||
else:
|
else:
|
||||||
|
@ -74,7 +82,9 @@ def parse_input(input_string, say_ok=True):
|
||||||
input_string = get_next(input_string)
|
input_string = get_next(input_string)
|
||||||
if say_ok:
|
if say_ok:
|
||||||
print("ok.")
|
print("ok.")
|
||||||
|
def add_new_word_to_string(string):
|
||||||
|
for char in string[::-1]:
|
||||||
|
stack.insert(0, ord(char))
|
||||||
def dump_heap():
|
def dump_heap():
|
||||||
print(heap)
|
print(heap)
|
||||||
|
|
||||||
|
@ -179,6 +189,14 @@ def add_to_new_word(value):
|
||||||
else:
|
else:
|
||||||
words[word_name] = words[word_name] + value + " "
|
words[word_name] = words[word_name] + value + " "
|
||||||
|
|
||||||
|
def start_reading_string():
|
||||||
|
global reading_string
|
||||||
|
reading_string = True
|
||||||
|
|
||||||
|
def stop_reading_string():
|
||||||
|
global reading_string
|
||||||
|
reading_string = False
|
||||||
|
|
||||||
def start_compiling():
|
def start_compiling():
|
||||||
global compiling
|
global compiling
|
||||||
compiling = True
|
compiling = True
|
||||||
|
|
Loading…
Reference in New Issue