Skip to content

Commit c402204

Browse files
committed
bcbc
1 parent 0253ec0 commit c402204

9 files changed

Lines changed: 30 additions & 24 deletions

File tree

stroll/lib/core.sr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(fn print value
2+
(native "print(value)")
3+
)

stroll/lib/fileIO.sr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(fn )

stroll/lib/string.sr

Whitespace-only changes.

stroll/src/cli.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,4 @@ def getcliargs() -> str:
88
result = ""
99
with open(args.FILENAME, "r") as f:
1010
result = f.read()
11-
12-
if not args.FILENAME.endswith(".sr"):
13-
print(Extensionerror(result, "Change the file extension"))
14-
terminate()
1511
return result

stroll/src/lexer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def tokenize(s: str):
2727
else:
2828
buf.append(s[i])
2929
i += 1
30+
#print(buf)
3031
if finished:
3132
tokens.append('"' + ''.join(buf) + '"')
3233
else:
@@ -48,6 +49,7 @@ def tokenize(s: str):
4849
else:
4950
buf.append(s[i])
5051
i += 1
52+
#print(buf)
5153
if finished:
5254
tokens.append("'" + "".join(buf) + "'")
5355
else:
@@ -59,5 +61,5 @@ def tokenize(s: str):
5961
j += 1
6062
tokens.append(s[i:j])
6163
i = j
62-
64+
#print(tokens, "tok")
6365
return tokens

stroll/src/nonnaiveparser.sr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(print "with open('file.txt') as f: x = f.read();print(x, ')')")
1+
(native "with open('file.txt') as f: x = f.read() ; print(x) ; print('The file content: ', x)")

stroll/src/parser.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,13 @@ def atom(token):
99
except ValueError:
1010
return str(token)
1111

12-
def parse(tokens, inquote=False, quotetype=None):
13-
print(tokens)
12+
def parse(tokens):
13+
#print(tokens)
1414
token = tokens.pop(0)
15-
16-
if token == "'" or token == '"' or token == "\'" or token == '\"':
17-
if inquote:
18-
if token == quotetype:
19-
inquote = False
20-
quotetype=None
21-
else:
22-
inquote = True
23-
quotetype == quotetype
24-
25-
if token == '(' and inquote==False:
15+
if token == '(':
2616
L = []
27-
2817
while tokens[0] != ')':
29-
L.append(parse(tokens, inquote, quotetype))
18+
L.append(parse(tokens))
3019
tokens.pop(0)
3120
return L
3221
else:

stroll/src/runtime.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def evaluate(ast, env={}):
55
"*", "/", "-", "+", "let", "print", "scan", "if", "elif", "else", "else","while",
66
"list", "append", "index", ">", "<", ">=", "<=", "==", "!=",
77
"true", "false", "&", "|", "sqrt", "pow", "mod", "abs",
8-
"len", "reverse", "concat", "strlen", "substr", "fn", "call"
8+
"len", "reverse", "concat", "strlen", "substr", "fn", "call", "native"
99
]
1010

1111
E = lambda x: evaluate(x, env)
@@ -183,7 +183,10 @@ def evaluate(ast, env={}):
183183

184184
if token == "fn": #fn define
185185
fnname = ast[idx + 1]
186-
fnvars = ast[idx + 2].split("|")# the format wil be like fn sayhi x|y|z code
186+
if "|" in ast[idx + 2]:
187+
fnvars = ast[idx + 2].split("|")# the format wil be like fn sayhi x|y|z code
188+
else:
189+
fnvars = ast[idx + 2]
187190
fnbody = ast[idx + 3:]
188191

189192
env[fnname] = [fnvars, fnbody]
@@ -201,6 +204,17 @@ def evaluate(ast, env={}):
201204
result = evaluate(expr, localenv)
202205
return result
203206

207+
if token == "native":
208+
code = evaluate(ast[1], env)
209+
if not isinstance(code, str):
210+
return code
211+
# Try expression first
212+
try:
213+
return eval(code, env) # expression
214+
except SyntaxError:
215+
exec(code, env) # statement/block
216+
return None
217+
204218
# ---- Base case: literals ----
205219
elif isinstance(ast, (int, float)):
206220
return ast

stroll/src/stroll.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
# --- prep program once ---
77
USERCODE = getcliargs()
88
src = []
9+
910
for raw_line in USERCODE.splitlines():
10-
code = raw_line.split(";", 1)[0]
11+
code = raw_line.split("!", 1)[0]
1112
if code.strip():
1213
src.append(code)
1314

0 commit comments

Comments
 (0)