-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.ts
39 lines (33 loc) · 1.43 KB
/
example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// deno-lint-ignore-file no-unused-vars
import TinyComp, {
AttributeGrammar,
LexicalRuleset,
SemanticRuleset,
SyntaxRuleset,
TinyCompOptions,
} from "./ts/TinyComp.ts";
import { exampleLexicalRuleset } from "./ts/attributeGrammar/lexicalRuleset.ts";
import { exampleSyntaxRuleset } from "./ts/attributeGrammar/syntaxRuleset.ts";
import { exampleSemanticRuleset } from "./ts/attributeGrammar/semanticRuleset.ts";
// ====================================================== //
// ======================= Example ====================== //
// ====================================================== //
// create an attribute grammar object with the example rulesets
const attributeGrammar: AttributeGrammar = {
lexicalRuleset: exampleLexicalRuleset,
syntaxRuleset: exampleSyntaxRuleset,
semanticRuleset: exampleSemanticRuleset,
};
// configure the compiler with the start symbol and the names of the tokens that should be ignored
const compilerOptions: TinyCompOptions = {
startSymbol: "PRINT_FUNCTION",
ignoreTokensNamed: ["whitespace"],
};
// instantiate the compiler and compile the input
const compiler = new TinyComp(attributeGrammar, compilerOptions);
const compileResult = compiler.compile(
`print("Hello World", "Optional Hello World")`
);
// execute the compiled code (in this case it is a function that prints "Hello World" and "Optional Hello World")"),
// but it could be any other JavaScript code like e.g. an object
compileResult();