73 lines
1.8 KiB
Plaintext
73 lines
1.8 KiB
Plaintext
![]() |
/ NGNK Parser and Source Code Generator
|
||
|
|
||
|
/ Define AST Structures
|
||
|
ASTNode:{(x;y;z)}
|
||
|
FunctionCall:{(x;y)}
|
||
|
Conditional:{(x;y;z)}
|
||
|
SystemCommand:{(x)}
|
||
|
|
||
|
/ Tokenizer using State Machine with Scan (Right-to-Left)
|
||
|
tokenize:{
|
||
|
states: ("num"; "id"; "op"; "ws"; "adverb");
|
||
|
s:();
|
||
|
x:|x;
|
||
|
|
||
|
classify:{
|
||
|
?x in "0123456789": "num"
|
||
|
; ?x in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz": "id"
|
||
|
; ?x in "()+-*/%=<>~,:#@.?;[]{}!": "op"
|
||
|
; ?x in "' / \\ ': /: \\:": "adverb"
|
||
|
; "ws"
|
||
|
}
|
||
|
|
||
|
tokens:states\:classify x;
|
||
|
s,::tokens;
|
||
|
|
||
|
if (#(s="ws") > 0) { s::ErrorNode["Invalid token detected"] }
|
||
|
|
||
|
s
|
||
|
}
|
||
|
|
||
|
/ Parsing Table
|
||
|
parseTable:(`+
|
||
|
`-`*`%`!`&`|`<`>`=`~`,`^`#`_`$`?`@`.
|
||
|
`binaryOp@)!
|
||
|
binaryOp@
|
||
|
parseTable[`$[]:`conditionalOp;
|
||
|
parseTable[`\`:systemCommandOp;
|
||
|
parseTable[`'`:adverbOp;
|
||
|
parseTable[`/`:adverbOp;
|
||
|
parseTable[`\\`:adverbOp;
|
||
|
parseTable[`':`:adverbOp;
|
||
|
parseTable[`/:`:adverbOp;
|
||
|
parseTable[`\\:`:adverbOp;
|
||
|
|
||
|
/ Operator Handling
|
||
|
binaryOp:{s::(x@-2;x@-1;*|_2#x); x:_2#x}
|
||
|
monadicOp:{s::(x@-1;*x); x:_1#x}
|
||
|
dyadicOp:{s::(x@-2;x@-1;*|_2#x); x:_2#x}
|
||
|
|
||
|
/ Specialized Operations
|
||
|
conditionalOp:{s::Conditional[x@-3;x@-2;x@-1]; x:_3#x}
|
||
|
systemCommandOp:{s::SystemCommand[x@-1]; x:_1#x}
|
||
|
adverbOp:{s::FunctionCall["adverb"; x@-1]; x:_1#x}
|
||
|
|
||
|
/ Parser
|
||
|
parse:{
|
||
|
s:();
|
||
|
x:|x;
|
||
|
s,::(t in parseTable ? parseTable[t] s : t like "0123456789" | t like '"*"' | t like '`*' ? t : FunctionCall[t;!s] if t like '*]')@\:x;
|
||
|
*s
|
||
|
}
|
||
|
|
||
|
/ Convert AST back to NGNK source code
|
||
|
generateSource:{
|
||
|
t:type x;
|
||
|
$[t=`FunctionCall; x 0, "[", ";" join generateSource each 1_x, "]"
|
||
|
;t=`Conditional; "$[", ";" join generateSource each x, "]"
|
||
|
;t=`SystemCommand; "\\", x 0
|
||
|
;x]
|
||
|
}
|
||
|
|
||
|
/ Example
|
||
|
code:"\\clear; x:2*3+4; while[x<20; x:x+2]; try[1/0]"; tokens:tokenize code; ast:parse tokens; `0:generateSource ast
|