Skip to content

Commit

Permalink
Basic Structure Support (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby authored Mar 22, 2024
1 parent 3969675 commit 68ea84c
Show file tree
Hide file tree
Showing 50 changed files with 2,989 additions and 726 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*.sa linguist-language=Salient
*.sa linguist-language=Salient eol=lf
* text=auto eol=lf
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"lock": false,
"nodeModulesDir": true,
"test": {
"include": ["tests/*/*", "source/*/*"]
"include": ["tests/**"]
}
}
21 changes: 21 additions & 0 deletions docs/language/structure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Structure

```bnf
struct ::= "struct" name struct_type? "{" struct_stmt* "}" ;
struct_type ::= %( ":" w* ) ...name %w* ;
struct_stmt ::= struct_attr | struct_spread ;
struct_attr ::= ...name ":" access ";" ;
struct_spread ::= "..." access ";" ;
```


## Memory Layout

| Struct Type | Attribute Storage Method | Gaps | Ordered |
| :-: | :- | :-: | :-: |
| Sparse | Stored in order with gaps between them to ensure each attribute is correctly aligned | Yes | Yes |
| Aligned | Stored with gaps to ensure alignment, however reorders such to minimise the required gaps | Yes | No |
| Linear | Stored in order with no gaps between attributes | No | Yes |
| Compact | Stored with no gaps in such an order as to maximise alignment | No | No |

*Defaults to sparse*
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"scripts": {
"build": "run-s build:*",
"build:syntax": "npx bnf-compile ./source/bnf/",
"build:compiler": "deno compile --output salient.exe --allow-read --allow-write --allow-env --allow-run --allow-sys ./source/cli.ts",
"build:compiler": "deno compile --output salient.exe -A ./source/cli.ts",
"test": "deno test",
"compile": "deno run --allow-read --allow-write --allow-env --allow-run --allow-sys ./source/cli.ts"
"compile": "deno run -A ./source/cli.ts"
},
"bin": {
"salient": "bin/cli.js"
Expand All @@ -36,7 +36,7 @@
"chalk": "^5.3.0"
},
"devDependencies": {
"bnf-parser": "^4.0.7",
"bnf-parser": "^4.1.0",
"npm-run-all": "^4.1.5",
"typescript": "^5.2.2"
}
Expand Down
50 changes: 34 additions & 16 deletions source/bnf/syntax.bnf
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
program ::= %w* ( stmt_top %w* )* ;
stmt_top ::=
function ;

stmt_top ::= function | structure ;


#=============================
Expand All @@ -14,6 +12,8 @@ digit ::= "0" -> "9" ;
digit_nz ::= "1" -> "9" ;
letter ::= "a" -> "z" | "A" -> "Z" ;

terminate ::= ( w* ";" w* );



#=============================
Expand Down Expand Up @@ -58,45 +58,63 @@ access ::= name ( %w* accessor )* ;
access_comp ::= %"#[]";

declare ::= %( "let" w* ) name %w* (%":" %w* access %w*)? ( %("=" w*) expr )? %(w* ";" w*) ;
assign ::= name %( w* "=" w*) expr %( w* ";" w* ) ;
assign ::= access %( w* "=" w*) expr %terminate ;



#=============================
# Storage
#=============================
structure ::= %("struct" w*) ...name %w* struct_type? %( "{" w* ) struct_stmt* %( w* "}" w* );
struct_type ::= %( ":" w* ) ...name %w* ;
struct_stmt ::= struct_attr | struct_spread ;
struct_attr ::= ...name %( w* ":" w* ) access %terminate ;
struct_spread ::= %( "..." ) access %terminate ;

container ::= %(w* "[" w*) ( container_item ( %(w* "," w*) container_item )* %w* %","? )? %("]" w*) ;
container_item ::= container_map | container_value ;
container_map ::= %"." name %(w* ":" w*) expr ;
container_value ::= expr ;


#=============================
# Function
#=============================
function ::= func_head %w* ( block | ";" ) ;
func_head ::= %("fn" w+) ...name %( w* "(" w* ) func_args %(w* ")" w* ":" w*) access ;
func_head ::= %("fn" w+) ...name %( w* "(" w* ) func_args %(w* ")" w*) %(":" w*) access ;
func_args ::= ( func_arg %w* ( %( "," w* ) func_arg )* )? ;
func_arg ::= ...name %( w* ":" w* ) access ;

block ::= %( "{" w* ) block_stmt* %( w* "}" w* ) ;
block_stmt ::= declare | assign | return | raise | statement ;
block_stmt ::= assign | declare | return | raise | statement ;

func_call ::= access func_call_body;
func_call_body ::= %( w* "(" w* ) ( expr %w* ( %( "," w* ) expr %w* )* )? %( ")" w* ) ;

return ::= %"return" "_tail"? %w+ expr %( ";" w* );
raise ::= %"raise" %w+ expr %( ";" w* );
return ::= %"return" "_call"? %w+ expr? %( ";" w* );
raise ::= %"raise" %w+ expr %( ";" w* ); # TODO rename to lift
# drop ::= %"drop" %w+ expr %( ";" w* );

#=============================
# Expression
#=============================
expr ::= expr_arg %w* ( ...expr_infix %w* expr_arg %w* )* ;
expr_prefix ::= "!" | "-" | "return" ;
expr_prefix ::= "!" | "-" ;
expr_infix ::= "&&" | "||" | "^" | "==" | "!=" | "<=" | ">=" | "<" | ">"
| "%" | "*" | "/" | "+" | "-"
| "as" | "instanceof"
| "->" ;
expr_postfix ::= expr_call | expr_get | expr_param ;
expr_param ::= %"#[" %w* arg_list %w* %"]" ;
expr_call ::= %"(" %w* arg_list %w* %")" ;
expr_get ::= %"[" %w* arg_list %w* %"]" ;
expr_arg ::= expr_prefix? %w* ( constant | expr_brackets | if | name | block ) %w* expr_postfix* ;
| "." | "->" ;
expr_postfix ::= expr_call | expr_get | expr_param | expr_loan ;
expr_param ::= %"#[" %w* arg_list %w* %"]" ;
expr_call ::= %"(" %w* arg_list %w* %")" ;
expr_get ::= %"[" %w* arg_list %w* %"]" ;
expr_loan ::= "@" | "$" ;
expr_arg ::= expr_prefix? %w* expr_val %w* expr_postfix* ;
expr_val ::= constant | expr_brackets | block | container | if | name ;
expr_brackets ::= %( "(" w* ) expr %( w* ")" ) ;

arg_list ::= ( expr %w* ","? %w* )* ;

if ::= %("if" w*) expr %w* expr %w* ( %"else" %w* expr )? ;

statement ::= expr %(w* ";"? w*) ;
statement ::= expr %terminate ;
Loading

0 comments on commit 68ea84c

Please sign in to comment.