Skip to content

Commit 0cac17a

Browse files
committed
Cleanups and grammar update
1 parent d394a34 commit 0cac17a

File tree

6 files changed

+124
-114
lines changed

6 files changed

+124
-114
lines changed

bootstrap/fing.defs

-5
This file was deleted.

bootstrap/test.foidl

-8
This file was deleted.

docs/grammer.txt

+65-64
Original file line numberDiff line numberDiff line change
@@ -3,92 +3,93 @@
33
; {} optional - zero or more
44
; [] zero or one
55
; 'term' terminal
6-
; non-term (no quotes) - non terminal
7-
; reg_ex(...) - regular expression
6+
; NON-TERMINAL
7+
; reg_ex(...) - regular
88

9-
S =: module
9+
S =: MODULE
1010

11-
module =: 'module' symbol {i_decls} {decltypes}
11+
MODULE =: 'module' SYMBOL {IDECLS} {DECLTYPES}
1212

13-
idecls =: 'include' (idecl_list | symbol_type)
14-
idelc_list =: '[' symbol {[,] symbol} ']'
13+
IDECLS =: 'include' (IDECL_LIST | SYMBOL)
1514

16-
decl_types =: {var_decl | func_decl}
15+
IDELC_LIST =: '[' SYMBOL {[,] SYMBOL} ']'
1716

18-
var_decl =: 'var' symbol_type [expression]
17+
DECLTYPES =: {VAR_DECL | FUNC_DECL}
1918

20-
func_decl =: 'func' symbol_type symbol_list {expression}
19+
VAR_DECL =: 'var' SYMBOL_TYPE [EXPRESSION]
2120

22-
expression =: literal
23-
| symbol_type
24-
| collection
25-
| func_call
26-
| if_expr
27-
| partial expr
28-
| group_expr
29-
| let_expr
30-
| match_expr
31-
| lambda_expr
21+
FUNC_DECL =: 'func' SYMBOL_TYPE SYMBOL_LIST {EXPRESSION}
3222

33-
func_call =: func_prefix expression {expression}
34-
func_prefix =: symbol_type ":"
23+
EXPRESSION =: LITERAL
24+
| SYMBOL_TYPE
25+
| COLLECTION
26+
| FUNC_CALL
27+
| IF_EXPR
28+
| PARTIAL EXPR
29+
| GROUP_EXPR
30+
| LET_EXPR
31+
| MATCH_EXPR
32+
| LAMBDA_EXPR
3533

36-
; Partials are interesting:
37-
; 1. If the symbol type is func reference and argument counts align, then it becomes a call
38-
; 2. If the symbol type is func reference and arg count in partial is less, it's a partial declaration
39-
; 3. If the symbol type is lambda reference and argument counts align, then it becomes a call
40-
; 4. If the symbol type is lambda reference and arg count in partial is less, it's a partial declaration
41-
; 5. Otherwise they are partial invocations
34+
FUNC_CALL =: FUNC_PREFIX EXPRESSION {EXPRESSION}
4235

43-
partial_expr =: "(" (func_prefix | symbol_type) {expression} ")"
36+
PARTIAL_EXPR =: "(" SYMBOL_TYPE {EXPRESSION} ")"
4437

45-
group_expr =: "@(" expression {expression} ")"
38+
GROUP_EXPR =: "@(" EXPRESSION {EXPRESSION} ")"
4639

47-
let_expr =: "let" [symbol_type] "[" symbol_type expression {symbol_type expression} "]" expression
40+
LET_EXPR =: "let" [SYMBOL_TYPE] "[" SYMBOL_TYPE EXPRESSION {SYMBOL_TYPE EXPRESSION} "]" EXPRESSION
4841

49-
match_expr =: 'match' [symbol_type] expression match_pair
42+
MATCH_EXPR =: 'match' [SYMBOL_TYPE] EXPRESSION MATCH_PAIR
5043

51-
match_pair =: '|' expression expression {match_pair}
52-
| ':default' expression
44+
MATCH_PAIR =: '|' EXPRESSION EXPRESSION {MATCH_PAIR}
45+
| ':default' EXPRESSION
5346

54-
; Lambdas my be interesting:
55-
; 1. If the body has not references to funcargs, letargs, letresult it's straight up
56-
; 2. Otherwise it is a closure. Closures need free variable management:
57-
; 1. Each free variable inserts into the begining of the declared symbol_list
58-
; 2. It needs to imbue those free vars at the point of declaration
47+
LAMBDA_EXPR =: '^' SYMBOL_LIST EXPRESSION
5948

60-
lambda_expr =: '^' symbol_list expression
49+
COLLECTION =: MAP_COLLECTION
50+
| LIST_COLLECTION
51+
| VECTOR_COLLECTION
52+
| SET_COLLECTION
6153

62-
symbol_list =: "[" "]"
63-
| "[" symbol_type {[,] symbol_type} "]"
64-
65-
symbol_type =: symbol ; foo
66-
| reg_ex([a-zA-Z][a-zA-Z0-9_]+!) ; fo_o!
67-
| reg_ex([a-zA-Z][a-zA-Z0-9_]+\?) ; f_oo?
68-
69-
symbol =: reg_ex([a-zA-Z]([a-zA-Z0-9_]*)?)
70-
71-
collection =: map_collection
72-
| list_collection
73-
| vector_collection
74-
| set_collection
75-
76-
map_collection =: "{" expression expression {[,] expression expression} "}"
54+
MAP_COLLECTION =: "{" EXPRESSION EXPRESSION {[,] EXPRESSION EXPRESSION} "}"
7755
| "{" "}"
7856

79-
list_collection =: "[" expression {[,] expression } "]"
57+
LIST_COLLECTION =: "[" EXPRESSION {[,] EXPRESSION } "]"
8058
| "[" "]"
8159

82-
set_collection =: "#{" expression {[,] expression} "}"
60+
SET_COLLECTION =: "#{" EXPRESSION {[,] EXPRESSION} "}"
8361
| "#{" "}"
8462

85-
vector_collection =: "<" expression {[,] expression} ">"
63+
VECTOR_COLLECTION =: "<" EXPRESSION {[,] EXPRESSION} ">"
8664
| "<" ">"
8765

88-
literal =: integer ; 1234
89-
| real ; 0.1, 19.52, 1.39489384938493849384983
90-
| hex ; 0x9ab5
91-
| bit ; 0b01010101
92-
| string ; "Something in quotes"
93-
| keyword ; :master
94-
| char ; 'c', 'd', '1'
66+
LITERAL =: INTEGER
67+
| REAL
68+
| HEX
69+
| BIT
70+
| STRING
71+
| KEYWORD
72+
| CHAR
73+
74+
SYMBOL_LIST =: "[" "]"
75+
| "[" SYMBOL_TYPE {[,] SYMBOL_TYPE} "]"
76+
77+
SYMBOL_TYPE =: SYMBOL
78+
| reg_ex("[a-zA-Z][a-zA-Z0-9_]+!")
79+
| reg_ex("[a-zA-Z][a-zA-Z0-9_]+\?")
80+
81+
SYMBOL =: reg_ex("[a-zA-Z]([a-zA-Z0-9_]*)?")
82+
83+
FUNC_PREFIX =: SYMBOL_TYPE ":"
84+
85+
INTEGER =: regex("-?[0-9]+")
86+
REAL =: regex("-?(?:0|[1-9]\d*)\.\d*(?:[eE][+\-]?\d+)?")
87+
HEX =: regex("0[xX][0-9a-fA-F]+")
88+
BIT =: regex("0[bB][0-1]+")
89+
STRING =: regex("\"(.|\n)*?\"")
90+
KEYWORD =: regex(":[a-zA-Z]([a-zA-Z0-9_]*)?")
91+
CHAR =: regex("\'[0-9a-zA-Z]*?\'")
92+
93+
COMMENT =: regex(";.*")
94+
IGNORES =: regex("[,\t ]+")
95+
NLCR =: regex("(\r\n|[\r\n])")

foidlc/fsrc/ast_type.foidl

+40-33
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,13 @@ var :private ast_type {
134134
TYPE :private}
135135
}
136136

137-
; Sets of things for utility
138-
var collection_set #{:empty_collection :collection}
139-
var coltype_set #{:list :map :set :vector}
140-
var littype_set #{:string :keyword :integer :real_number :hex :bin :char}
141-
var decl_set #{:function :variable :include :module}
142-
var ref_set #{:literalref :funcref :varref}
137+
; Convenience sets for predicates
138+
139+
var collection_set #{:empty_collection :collection}
140+
var coltype_set #{:list :map :set :vector}
141+
var littype_set #{:string :keyword :integer :real_number :hex :bin :char}
142+
var decl_set #{:function :variable :include :module}
143+
var ref_set #{:literalref :funcref :varref}
143144

144145
var name_set #{
145146
:module
@@ -156,12 +157,9 @@ var invalid_set #{
156157
:function
157158
:variable
158159
:include
159-
:module
160-
:match_exprref}
160+
:module}
161161

162-
; Function:
163-
; Description:
164-
; Syntax:
162+
; Predicates
165163

166164
func is_decl [node]
167165
getd: decl_set get: node TYPE false
@@ -186,17 +184,9 @@ func has_exprs? [node]
186184
false
187185
true
188186

189-
; Function:
190-
; Description:
191-
; Syntax:
192-
193187
func is_not_decl? [node]
194188
not: getd: decl_set get: node TYPE false
195189

196-
; Function:
197-
; Description:
198-
; Syntax:
199-
200190
func is_funclambda_args? [node]
201191
?: getd: collection_set get: node TYPE false
202192
?: getd: coltype_set get: node COLLTYPE false
@@ -213,12 +203,13 @@ func is_funclambda_args? [node]
213203
; token - The source file token (has sourcepos, etc.)
214204

215205
func type_instance [typekw source token]
216-
let typeinst [] getd: ast_type typekw nil
206+
let typeinst []
207+
getd: ast_type typekw nil
217208
?: nil?: typeinst
218209
failWith: extend: "Unrecognized AST type " typekw
219210
let []
220211
map_extend!: ; Extend new instance with token
221-
extendKV: typeinst SOURCE source ; Create new instance and extend
212+
extendKV: typeinst SOURCE source ; Create new instance and extend
222213
TOKEN token
223214

224215
; Function: type_with_exprs
@@ -232,29 +223,45 @@ func type_with_exprs [typekw source token]
232223
let node [] type_instance: typekw source token
233224
map_extend!: node EXPRS list_inst!:
234225

226+
; Function: type_literal_ref
227+
; Description: Constructs a literal reference storing the
228+
; base type in modules literal map
229+
235230
func type_literal_ref [mmodule token source]
236-
let ttype [] get: token :token_type
237-
let lmap [] get: get: mmodule LITERALS ttype
238-
let lref [] type_instance: :literalref source token
239-
map_extend!: lref LITTYPE ttype
240-
map_extend!: lmap get: token :token_str lref
231+
let ttype []
232+
get: token :token_type
233+
; Create the literal reference
234+
let lref []
235+
map_extend!:
236+
type_instance: :literalref source token
237+
LITTYPE ttype
238+
; Add to the global (module) literal map
239+
map_extend!:
240+
get: get: mmodule LITERALS ttype
241+
get: token :token_str lref
242+
; Return the literal reference
241243
lref
242244

243245
; Function:
244246
; Description:
245247
; Syntax:
246248

247249
func :private gen_literal_map [mnode]
248-
fold:
249-
^[acc x]
250-
map_extend!: acc x map_inst!:
251-
map_inst!: littype_set
250+
fold:
251+
^[acc x]
252+
map_extend!: acc x map_inst!:
253+
map_inst!: littype_set
254+
255+
; Function:
256+
; Description:
257+
; Syntax:
252258

253259
func type_module_create [source token]
254-
let mnode [] type_with_exprs: :module source token
260+
let mnode []
261+
type_with_exprs: :module source token
262+
255263
map_extend!:
256264
map_extend!:
257265
mnode
258-
LITERALS
259-
gen_literal_map: mnode
266+
LITERALS gen_literal_map: mnode
260267
EXTERNS map_inst!:

foidlc/fsrc/foidlc.foidl

+10-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ module foidlc
2323
include cmdline
2424
include handler
2525

26-
var version "foidlc version 0.01.00 (Self-hosted)"
26+
var version "foidlc version 0.01.00 (Self-hosted)"
27+
28+
; Function: execute
29+
; Description: Core compiler execution. If version (-v)
30+
; was not specified on the command line, either
31+
; a header generation (-g) or compilation (-c) is requested
32+
; Help (-h) is handled by cmdlineparse
2733

2834
func :private execute [argv cmap]
2935
?: get: cmap :version
@@ -32,5 +38,8 @@ func :private execute [argv cmap]
3238
build_handler_and_run: cmap
3339
cmap
3440

41+
; Function: main
42+
; Description: Main entry point to program/application
43+
3544
func main [argv]
3645
execute: argv cmdlineparse: argv

tests/data/simple.foidl

+9-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ module simple
2424
func fauxfunc [a b]
2525
+: a b
2626

27+
func noarg []
28+
9
29+
2730
func foo [arg1]
28-
let a [b 5]
29-
+: arg1 b
31+
match arg1
32+
| 1 fauxfunc: %0 9
33+
| 2 let a [b 5]
34+
+: arg1 noarg:
35+
36+
3037

31-
fauxfunc: a 9

0 commit comments

Comments
 (0)