Compare commits
2 commits
92a365d908
...
a7034db0eb
Author | SHA1 | Date | |
---|---|---|---|
a7034db0eb | |||
c2678fe360 |
4 changed files with 79 additions and 18 deletions
|
@ -5,6 +5,7 @@ import io
|
||||||
|
|
||||||
// main can return int or void, void mains are handled by jortsc
|
// main can return int or void, void mains are handled by jortsc
|
||||||
fn main () -> int {
|
fn main () -> int {
|
||||||
io.puts("pants")
|
// todo: put it back to io.puts
|
||||||
|
ioputs("pants")
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,3 +27,19 @@ class Identifier:
|
||||||
@dataclass
|
@dataclass
|
||||||
class Import:
|
class Import:
|
||||||
module: str
|
module: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class String:
|
||||||
|
value: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Number:
|
||||||
|
value: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class FunctionCall:
|
||||||
|
function: str
|
||||||
|
args: list
|
||||||
|
|
|
@ -13,6 +13,7 @@ class TokenType(Enum):
|
||||||
comment_end = auto()
|
comment_end = auto()
|
||||||
whitespace = auto()
|
whitespace = auto()
|
||||||
number = auto()
|
number = auto()
|
||||||
|
string = auto()
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
@ -50,7 +51,7 @@ TOKENS = [
|
||||||
(r'\-\>', TokenType.reserved),
|
(r'\-\>', TokenType.reserved),
|
||||||
(r'\.', TokenType.reserved),
|
(r'\.', TokenType.reserved),
|
||||||
|
|
||||||
(r'\"[^\n]*\"', TokenType.reserved),
|
(r'\"[^\n]*\"', TokenType.string),
|
||||||
|
|
||||||
# basic math ops
|
# basic math ops
|
||||||
(r'[\+\-\/\*]', TokenType.reserved),
|
(r'[\+\-\/\*]', TokenType.reserved),
|
||||||
|
|
|
@ -2,7 +2,8 @@ from typing import Optional, Any, List
|
||||||
|
|
||||||
from jortsc.parser.lexer import Token, TokenType
|
from jortsc.parser.lexer import Token, TokenType
|
||||||
from jortsc.parser.ast_nodes import (
|
from jortsc.parser.ast_nodes import (
|
||||||
Function, TypedVar, Identifier, Import, ReturnType
|
Function, TypedVar, Identifier, Import, ReturnType, String, Number,
|
||||||
|
FunctionCall
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,7 +19,8 @@ class Reader:
|
||||||
self.cur = 0
|
self.cur = 0
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'<Reader cur={self.cur} cur_tok={self.peek()}>'
|
return (f'<Reader cur={self.cur} tot={len(self.tokens)} '
|
||||||
|
f'cur_tok={self.peek()}>')
|
||||||
|
|
||||||
def peek(self) -> Optional[Token]:
|
def peek(self) -> Optional[Token]:
|
||||||
"""Peek at the current token."""
|
"""Peek at the current token."""
|
||||||
|
@ -168,6 +170,34 @@ def read_reserved(token: Token, reader: Reader):
|
||||||
return handler(reader)
|
return handler(reader)
|
||||||
|
|
||||||
|
|
||||||
|
def read_value(token: Token, _reader: Reader):
|
||||||
|
"""Read a given value"""
|
||||||
|
if token.type_ == TokenType.string:
|
||||||
|
return String(token.value)
|
||||||
|
elif token.type_ == TokenType.number:
|
||||||
|
return Number(token.value)
|
||||||
|
|
||||||
|
|
||||||
|
def read_statement(token: Token, reader: Reader):
|
||||||
|
"""Read a statement"""
|
||||||
|
# token is an identifier, so first check for a function call
|
||||||
|
|
||||||
|
# TODO: handle more things than a function call
|
||||||
|
call_fn_name = token.value
|
||||||
|
token = reader.expect_val('(')
|
||||||
|
res = []
|
||||||
|
|
||||||
|
while True:
|
||||||
|
token = reader.next_safe()
|
||||||
|
|
||||||
|
if token.value == ')':
|
||||||
|
break
|
||||||
|
|
||||||
|
res.append(read_value(token, reader))
|
||||||
|
|
||||||
|
return FunctionCall(call_fn_name, res)
|
||||||
|
|
||||||
|
|
||||||
def read_start(reader: Reader):
|
def read_start(reader: Reader):
|
||||||
"""Read the start of a program."""
|
"""Read the start of a program."""
|
||||||
print('reader', reader)
|
print('reader', reader)
|
||||||
|
@ -175,22 +205,33 @@ def read_start(reader: Reader):
|
||||||
token = reader.next()
|
token = reader.next()
|
||||||
|
|
||||||
if token is None:
|
if token is None:
|
||||||
return []
|
print('eof!')
|
||||||
|
return None
|
||||||
|
|
||||||
ast = []
|
ast = []
|
||||||
res = []
|
res = []
|
||||||
|
|
||||||
print('cur', token)
|
|
||||||
|
|
||||||
# handle blocks
|
# handle blocks
|
||||||
if token.value == '{':
|
if token.value == '{':
|
||||||
while True:
|
# next can be a whitespace, or a }
|
||||||
token = reader.next()
|
token = reader.next()
|
||||||
|
|
||||||
if token.value == '}':
|
print('block start!, next:', token)
|
||||||
break
|
|
||||||
|
|
||||||
res.extend(read_start(reader))
|
if token.type_ == TokenType.whitespace:
|
||||||
|
# keep going on reading
|
||||||
|
while True:
|
||||||
|
token = reader.peek()
|
||||||
|
print('block append', token)
|
||||||
|
|
||||||
|
if token.value == '}':
|
||||||
|
print('block end')
|
||||||
|
reader.next()
|
||||||
|
break
|
||||||
|
|
||||||
|
res.extend(read_start(reader))
|
||||||
|
elif token.value == '}':
|
||||||
|
res = []
|
||||||
|
|
||||||
# import, fn, etc
|
# import, fn, etc
|
||||||
elif token.type_ == TokenType.reserved:
|
elif token.type_ == TokenType.reserved:
|
||||||
|
@ -200,24 +241,26 @@ def read_start(reader: Reader):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
elif token.type_ == TokenType.identifier:
|
elif token.type_ == TokenType.identifier:
|
||||||
res = [Identifier(token.value)]
|
res = read_statement(token, reader)
|
||||||
|
else:
|
||||||
|
res = read_value(token, reader)
|
||||||
|
|
||||||
ast.append(res)
|
ast.append(res)
|
||||||
return ast
|
return ast
|
||||||
|
|
||||||
|
|
||||||
def read_loop(reader: Reader):
|
def read_loop(reader: Reader):
|
||||||
|
"""Read the AST."""
|
||||||
final_ast = []
|
final_ast = []
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
ast = read_start(reader)
|
ast = read_start(reader)
|
||||||
|
|
||||||
if not ast:
|
# break when eof
|
||||||
|
if ast is None:
|
||||||
break
|
break
|
||||||
|
|
||||||
inner = ast[0]
|
# TODO: better ast cleanup
|
||||||
|
|
||||||
if not inner:
|
|
||||||
break
|
|
||||||
|
|
||||||
final_ast.append(ast)
|
final_ast.append(ast)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue