Change syntax of compound exprs

This commit is contained in:
Krzosa Karol
2022-05-29 23:42:53 +02:00
parent 3f44a533be
commit 802dce749e
7 changed files with 65 additions and 75 deletions

View File

@@ -285,7 +285,7 @@ gen_ast(Ast *ast){
else if(sym->type == type_type){
if(sym->type_val->kind == TYPE_STRUCT){
Ast_Struct *agg = const_get_struct(sym->type_val->sym->ast);
if(agg){
if(node->value->kind == AST_STRUCT){
gen("struct %s{", node->name.str);
global_indent++;
For(agg->members){

View File

@@ -22,14 +22,14 @@ bool_null: bool = null
//-----------------------------------------------------------------------------
// Compound expressions
//-----------------------------------------------------------------------------
array1: [4]int = {1,2,3,4}
array2: [32]int = {1,2,3,4}
array3: [32]int = {
array1: [4]int = [4]int(1,2,3,4)
array2 := [32]int(1,2,3,4)
array3 := [32]int(
[0] = null,
[1] = 1,
[2] = 2,
[31] = 31,
}
)
array_item := array1[0]
array_item_imp: int = array2[2]

View File

@@ -26,13 +26,15 @@
/// @todo
/// [x] - Typespecs should probably be expressions so stuff like would be possible :: *[32]int
/// [ ] - Lexer: Need to insert scope endings when hitting End of file
/// [ ] - Add single line lambda expressions/
/// [ ] - Think about compound expressions, unify with calls - maybe Thing(a=1) instead of Thing{a=1}
/// [x] - Initial order independence algorithm
/// [ ] - Cleanup the mess with constant bindings
/// [ ] - Structs
/// [ ] - Enums
/// [ ] - For loop
/// [ ] - Switch
/// [ ] - Think about compound expressions, unify with calls - maybe Thing(a=1) instead of Thing{a=1}
/// [ ] - Lexer: Need to insert scope endings when hitting End of file
/// [ ] - Add single line lambda expressions/
int main(){
@@ -47,12 +49,12 @@ int main(){
test_intern_table();
lex_test();
// String result = compile_file("globals.kl"_s);
String result = compile_file("structs.kl"_s);
// String result = compile_file("order_independent_globals.kl"_s);
String result = {};
// result = compile_file("order1.kl"_s);
// result = compile_file("lambdas.kl"_s);
// result = compile_file("order2.kl"_s);
result = compile_file("globals.kl"_s);
printf("%s", result.str);
// compile_file("lambdas.kl"_s);
// compile_file("globals.kl"_s);
__debugbreak();
}

View File

@@ -136,12 +136,12 @@ Compound literals
function Ast_Expr *parse_expr(S64 rbp = 0);
function Ast_Compound *
parse_expr_compound(){
parse_expr_compound(Ast_Expr *left){
Scratch scratch;
Token *pos = token_get();
Array<Ast_Compound_Item *> exprs = {scratch};
while(!token_is(TK_CloseBrace)){
while(!token_is(TK_CloseParen)){
Token *token = token_get();
Ast_Expr *index = 0;
Ast_Atom *name = 0;
@@ -164,9 +164,9 @@ parse_expr_compound(){
break;
}
}
token_expect(TK_CloseBrace);
token_expect(TK_CloseParen);
Ast_Compound *result = ast_expr_compound(pos, 0, exprs);
Ast_Compound *result = ast_expr_compound(pos, left, exprs);
return result;
}
@@ -287,12 +287,14 @@ null_denotation(Token *token){
case TK_Integer : return ast_int(token, token->int_val, AST_EXPR);
case TK_Pointer : return ast_expr_unary(token, TK_Pointer, parse_expr());
case TK_Dereference: return ast_expr_unary(token, TK_Dereference, parse_expr());
case TK_OpenBracket: {
Ast_Array *result = ast_array(token, parse_expr());
token_expect(TK_CloseBracket);
result->base = parse_expr();
result->base = parse_expr(1);
return result;
}break;
case TK_Keyword: {
if(token->intern_val == keyword_cast){
token_expect(TK_OpenParen);
@@ -307,7 +309,7 @@ null_denotation(Token *token){
return 0;
}
}break;
case TK_OpenBrace: return parse_expr_compound();
case TK_OpenParen: {
if (token_is(TK_CloseParen)) return parse_lambda(token);
else if(token_is(TK_Identifier) && token_is(TK_Colon, 1)) return parse_lambda(token);
@@ -330,14 +332,6 @@ left_binding_power(Token_Kind kind){
}
}
function S64
postfix_binding_power(Token_Kind kind){
switch(kind){
case TK_Increment: case TK_Decrement: case TK_OpenBracket: return 1;
default: return 0;
}
}
function Ast_Expr *
left_denotation(Token *op, Ast_Expr *left){
enum{ Left_Associative, Right_Associative };
@@ -349,6 +343,14 @@ left_denotation(Token *op, Ast_Expr *left){
}
}
function S64
postfix_binding_power(Token_Kind kind){
switch(kind){
case TK_Decrement: case TK_Increment: case TK_OpenBracket: case TK_OpenParen: return 1;
default: return 0;
}
}
function Ast_Expr *
parse_expr(S64 rbp){
Token *token = token_next();
@@ -356,19 +358,25 @@ parse_expr(S64 rbp){
for(;;){
token = token_get();
if((rbp == 0) && (token->kind == TK_Increment || token->kind == TK_Decrement || token->kind == TK_OpenBracket)){
if(postfix_binding_power(token->kind) > rbp){
token_next();
if(token->kind == TK_OpenBracket){
// @note: parse postfix
switch(token->kind){
case TK_OpenBracket:{
Ast_Expr *index = parse_expr();
left = ast_expr_index(token, left, index);
token_expect(TK_CloseBracket);
}
else{
}break;
case TK_OpenParen:{
left = parse_expr_compound(left);
}break;
default:{
if(token->kind == TK_Increment) token->kind = TK_PostIncrement;
else if(token->kind == TK_Decrement) token->kind = TK_PostDecrement;
left = ast_expr_unary(token, token->kind, left);
}
}
}
else if(rbp < left_binding_power(token->kind)){
token = token_next();

17
order1.kl Normal file
View File

@@ -0,0 +1,17 @@
other_func :: ()
a_val := recursive_lambda
recursive_lambda :: (thing: int)
in_val := recursive_lambda
some_value := thing + const_in_lambda
const_in_lambda :: 10
not_const := val + 10
val := 10
DEPENDENCE :: CONSTANT_VAL
CONSTANT_VAL :: 10

View File

@@ -1,3 +1,4 @@
Str16 :: String16
arena_pointer: *Arena = null
thing: Arena
@@ -21,4 +22,3 @@ pointer := &with_type
deref := *pointer
Str16 :: String16

View File

@@ -1,37 +0,0 @@
/*
@todo: !!!
Maybe instead of compound exprs like this:
[4]Thing{1,2,3,4}
Do it like this
[4]Thing(1,2,3,4)
[4]Thing([0]=1, [3]=3)
Thing(a=1, b=2)
unifying the call expression and compound expression
seems more unified and better for coherence
but more abigous and probably harder to implement
value :: call((thing: int): int thing += 1; return thing, )
*/
other_func :: ()
a_val := recursive_lambda
recursive_lambda :: (thing: int)
in_val := recursive_lambda
some_value := thing + const_in_lambda
const_in_lambda :: 10
not_const := val + 10
val := 10
DEPENDENCE :: CONSTANT_VAL
CONSTANT_VAL :: 10