diff --git a/examples/add.jt b/examples/add.jt deleted file mode 100644 index 447355f..0000000 --- a/examples/add.jt +++ /dev/null @@ -1,23 +0,0 @@ -import io - -fn add (int a, int b) -> int { - a + b -} - -// return type is void by default -fn main () { - // explicit types, or - int val = add(2, 2) - - // type inferred from the functions' return value - val := add(2, 2) - - // variables are immutable, however, you can update them with - // the value of the old one. - val = val + 1 - - // a shorthand is val++, same for val--. - - // string interpolation is implicit - io.puts("2 plus 2 = {val}") -} diff --git a/examples/closures.jt b/examples/closures.jt deleted file mode 100644 index 6a1291a..0000000 --- a/examples/closures.jt +++ /dev/null @@ -1,22 +0,0 @@ -import io - -fn main () { - x := 0 - - // since variable are immutable but updatable, x is 1 inside clojure, but - // 0 inside main() - fn inner() { - x++ - } - - inner() - - // shows 0 - io.puts("x is {x}") - - // however, if you explicitly update x: - x = inner() - - // shows 1 - io.puts("x is {x}") -} diff --git a/examples/custom-types.jt b/examples/custom-types.jt deleted file mode 100644 index 534e670..0000000 --- a/examples/custom-types.jt +++ /dev/null @@ -1,11 +0,0 @@ -import io - -// you can create your own types with 'type' -type T = int - -fn main () { - T a = 2 - - // since T is int, io.puts with an int works - io.puts(a) -} diff --git a/examples/extending-structs.jt b/examples/extending-structs.jt deleted file mode 100644 index 3d8d8bc..0000000 --- a/examples/extending-structs.jt +++ /dev/null @@ -1,15 +0,0 @@ - -struct A { - int a, - int b -} - -struct B <- A { - int c -} - -fn main () { - a := A{1, 2} - b := B{1, 2, 3} -} - diff --git a/examples/function-overload.jt b/examples/function-overload.jt deleted file mode 100644 index e47830a..0000000 --- a/examples/function-overload.jt +++ /dev/null @@ -1,15 +0,0 @@ -import io -import integer - -fn my_puts(string str) { - io.puts(str) -} - -fn my_puts(int my_int) { - io.puts(integer.to_str(my_int)) -} - -fn main () { - my_puts(2) - my_puts("aaa") -} diff --git a/examples/hello.jt b/examples/hello.jt deleted file mode 100644 index 4bc50f6..0000000 --- a/examples/hello.jt +++ /dev/null @@ -1,11 +0,0 @@ -import io - -// if a return type is not defined, it is implicitly void and so the function -// returns nil (the only instance of void) - -// main can return int or void, void mains are handled by jortsc -fn main () -> int { - // todo: put it back to io.puts - ioputs("pants") - 0 -} diff --git a/examples/higher-order-functions.jt b/examples/higher-order-functions.jt deleted file mode 100644 index 34776dd..0000000 --- a/examples/higher-order-functions.jt +++ /dev/null @@ -1,26 +0,0 @@ -import io - -// takes a function that receives two ints, returns an int -// Func is the function type keyword, to not switch it with fn (which declares -// a function) -fn function_tester (Func func ([int, int] -> int)) -> int { - func(2, 2) -} - -fn add(int a, int b) -> int { - a + b -} - -fn main () { - // passes the function add to function_tester - res := function_tester(add) - - // you can also create functions and put them in variables. not putting a - // function name on the fn block makes it return a Func instance to be put - // in a variable - anonymous := (fn () {}) - - // anonymous has type Func ([] -> void) - - io.puts("res = {res}") -} diff --git a/examples/sockets.jt b/examples/sockets.jt deleted file mode 100644 index 08ab425..0000000 --- a/examples/sockets.jt +++ /dev/null @@ -1,10 +0,0 @@ -import socket -import io - -fn main () { - sock := socket.tcp_connect("example.com", 80) - sock.send("HTTP/1.1\n") - frame := sock.recv(1024) - sock.close() - io.puts(frame) -} diff --git a/examples/strings.jt b/examples/strings.jt deleted file mode 100644 index 75f0931..0000000 --- a/examples/strings.jt +++ /dev/null @@ -1,15 +0,0 @@ -import io - -fn main () { - s := "this is a string" - io.puts(s) - - s := "this is {s}" - io.puts(s) - - s := s + 2 // invalid - - // this however, is valid, there is an io.puts that handles int, - // more on function overload in a bit - io.puts(2) -} diff --git a/examples/struct-functions.jt b/examples/struct-functions.jt deleted file mode 100644 index 38df73e..0000000 --- a/examples/struct-functions.jt +++ /dev/null @@ -1,60 +0,0 @@ -import io - -struct A { - int val1, - int val2 -} - -// self is injected and represents the struct A -// from the functions' definition -fn A:sum_fields() -> int { - self.val1 + self.val2 -} - -// type of sum_fields is: -// Func ([A] -> int) - -// the mut keyword signals that self is a "reference" -// to self, instead of a copy - -// however, what actually happens is that an instance of -// A is returned from the function implicitly - -fn mut A:incr_both_fields() { - self.val1++ - self.val2++ -} - -// and so, the type becomes: -// Func ([A] -> A) - -fn mut A:incr_and_sum () { - self.val1++ - self.val2++ - - self.val1 + self.val2 -} - -// type is: -// Func ([A] -> (A, int)) - -fn main () { - a := A{0, 0} - - a.incr_both_fields() - - /* - translates to: - a := incr_both_fields(a) - */ - - sum := a.sum_fields() - io.puts(sum) - - val = a.incr_and_sum() - - /* - translates to: - a, val := incr_and_sum(a) - */ -} diff --git a/examples/structs.jt b/examples/structs.jt deleted file mode 100644 index 229058d..0000000 --- a/examples/structs.jt +++ /dev/null @@ -1,14 +0,0 @@ -import io - -struct MyStruct { - int var1, - int var2, - int var3 -} - -fn main () { - st = MyStruct{1, 2, 3} - - // TODO: define a way for printable things - io.puts(st) -} diff --git a/jortsc/main.py b/jortsc/main.py index 16d1ec2..a1b1820 100644 --- a/jortsc/main.py +++ b/jortsc/main.py @@ -5,23 +5,21 @@ import pprint import logging from jortsc.parser.lexer import lex_jorts -from jortsc.parser.syntatic import syntatic +# from jortsc.parser.parser import parse logging.basicConfig(level=logging.DEBUG) def main(): """main entry point""" try: - in_data = sys.stdin.read() + in_data = sys.stdin.read().strip() except EOFError: pass + print(repr(in_data)) tokens = lex_jorts(in_data) pprint.pprint(tokens) - tree = syntatic(tokens) - print(tree) - if __name__ == '__main__': main() diff --git a/jortsc/parser/parser.py b/jortsc/parser/parser.py deleted file mode 100644 index 7333bd6..0000000 --- a/jortsc/parser/parser.py +++ /dev/null @@ -1,44 +0,0 @@ - -from lark import Lark - -GRAMMAR = """ -FN: "fn" -IMPORT: "import" -COMMA: "," -DOT: "." -SINGLE_COMMENT: "//" -NEWLINE: /(\\r?\\n)+\\s*/ -ANY: /.+/ -WHITESPACE: " " -INTEGER: /[0-9]+/ -ARROW: "->" -COM_START: "/*" -COM_END: "*/" -QUOTE: "\\"" - -identifier: WHITESPACE* ANY WHITESPACE* - -single_comment: SINGLE_COMMENT ANY* NEWLINE -multi_comment: COM_START ANY* COM_END - -import_stmt: IMPORT identifier NEWLINE - -fn_arg: identifier identifier -parameters: fn_arg (COMMA fn_arg) -fn_stmt: FN identifier? "(" parameters? ")" [ARROW identifier] "{" NEWLINE? [stmt NEWLINE]* "}" - -sign_int: "+" | "-" -string: QUOTE ANY* QUOTE -value: (sign_int* INTEGER) | string - -call_stmt: [identifier DOT] identifier "(" [value COMMA]* ")" - -stmt: value | import_stmt | fn_stmt | call_stmt - -start: (NEWLINE | stmt)* -""" - -def parse(string: str): - """Parse using Lark""" - parser = Lark(GRAMMAR, parser='lalr', debug=True) - return parser.parse(string)