remove examples and main parser grammar

moving to lox described in https://craftinginterpreters.com
to go with. better learn something first, then walk towards things like
a static typed lang lol

anyways if you were here for jorts as my own language thing
do leave
This commit is contained in:
Luna 2019-05-31 16:04:09 -03:00
parent a7034db0eb
commit 3d26da0144
13 changed files with 3 additions and 271 deletions

View File

@ -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}")
}

View File

@ -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}")
}

View File

@ -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)
}

View File

@ -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}
}

View File

@ -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")
}

View File

@ -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
}

View File

@ -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}")
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
*/
}

View File

@ -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)
}

View File

@ -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()

View File

@ -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)