Skip to content

Commit 210ff5e

Browse files
committed
Fixes, kvtable support
* Added hexadecimal support to the tokenizer * Fixed tokenizer picking up function names with keywords in them as keywords done -> do * Added vector4 * Added kvtable instruction which transpiles directly to a lua table. * Added and, or, mod, greater than, less than ops
1 parent ada6ec5 commit 210ff5e

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

script.e2

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,16 @@ try {
4545
T = table()
4646
T[1, vector] = vec()
4747

48-
print( T[1, vector] )
48+
print( T[1, vector] )
49+
50+
local Tbl = table(
51+
"width" = Width,
52+
"height" = Height,
53+
"done" = 0,
54+
"output" = table(),
55+
"png" = 1,
56+
"type" = Type,
57+
58+
"crc" = 0,
59+
"adler" = 1
60+
)

src/Base/Parser.hx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,9 @@ class Parser {
454454
var type = "number";
455455

456456
var name = this.getTokenRaw();
457-
458457
if (name == null)
459458
this.error("Variable required");
460459

461-
var first_char = name.charAt(0);
462-
if (first_char != first_char.toUpperCase())
463-
this.error("Variable must start with uppercased letter");
464-
465460
if (used_vars.exists(name))
466461
this.error('Variable \'$name\' is already used as an argument,');
467462

@@ -486,16 +481,12 @@ class Parser {
486481
if (this.hasTokens()) {
487482
var vars: Array<String> = [];
488483
while (true) {
489-
if (this.acceptRoamingToken("lower_ident") || this.acceptRoamingToken("ident")) {
484+
if (this.acceptRoamingToken("ident")) {
490485
var name = this.getTokenRaw();
491486

492487
if (name == null)
493488
this.error("Variable required");
494489

495-
var first_char = name.charAt(0);
496-
if ( first_char != first_char.toUpperCase() )
497-
this.error("Variable must start with uppercased letter");
498-
499490
if ( used_vars.exists(name) )
500491
this.error('Variable \'$name\' is already used as an argument');
501492

@@ -620,7 +611,7 @@ class Parser {
620611

621612
keytype = this.getTokenRaw();
622613

623-
var typ = wire_expression_types.get(keytype.toUpperCase());
614+
var typ = wire_expression_types.get(keytype);
624615
if (typ == null)
625616
this.error('Unknown type: $keytype');
626617

@@ -890,7 +881,6 @@ class Parser {
890881
if (first_char != first_char.toLowerCase())
891882
this.error("Function name must start with a lower case letter", name_token);
892883

893-
894884
if ( !this.acceptRoamingToken("grammar", "(") )
895885
this.error("Left parenthesis (() must appear after function name");
896886

src/Base/Tokenizer.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ class Tokenizer {
138138

139139
new TokenMatch( "grammar", ~/{|}|,|;|:|\(|\)|\[|\]/, TokenType.Grammar),
140140

141-
new TokenMatch( "keyword", ~/elseif|if|else|break|continue|local|while|switch|case|default|try|catch|foreach|for|function|return|do/, TokenType.Keyword ),
141+
new TokenMatch( "keyword", ~/\belseif|if|else|break|continue|local|while|switch|case|default|try|catch|foreach|for|function|return|do\b/, TokenType.Keyword ),
142142

143143
new TokenMatch( "string", ~/("[^"\\]*(?:\\.[^"\\]*)*")/, TokenType.Literal, TokenFlag.None, function(token, pattern) {
144144
token.literal = E2Type.String( pattern.matched(0) );
145145
}),
146146

147-
new TokenMatch( "number", ~/-?(\d*\.)?\d+/, TokenType.Literal, TokenFlag.None, function(token, pattern) {
147+
new TokenMatch( "number", ~/-?(0[xX][0-9a-fA-F]+)|(-?(\d*\.)?\d+)/, TokenType.Literal, TokenFlag.None, function(token, pattern) {
148148
// In the future make this allow for rust strings etc
149149
var value = Std.parseFloat( pattern.matched(0) );
150150
if (Math.isNaN(value))

src/Base/Transpiler/Lua.hx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ class Instructions {
140140
static function instr_not(v: Instruction)
141141
return 'not ${ callInline(v) }';
142142

143+
static function instr_and(v1: Instruction, v2: Instruction)
144+
return '${ callInline(v1) } and ${ callInline(v2) }';
145+
146+
static function instr_or(v1: Instruction, v2: Instruction)
147+
return '${ callInline(v1) } or ${ callInline(v2) }';
148+
149+
static function instr_mod(v1: Instruction, v2: Instruction)
150+
return '${ callInline(v1) } % ${ callInline(v2) }';
151+
143152
static function instr_add(v: Instruction, addend: Instruction)
144153
return '${ callInline(v) } + ${ callInline(addend) }';
145154

@@ -164,6 +173,14 @@ class Instructions {
164173
static function instr_geq(v1: Instruction, v2: Instruction)
165174
return '${ callInline(v1) } >= ${ callInline(v2) }';
166175

176+
// Greater than
177+
static function instr_gt(v1: Instruction, v2: Instruction)
178+
return '${ callInline(v1) } > ${ callInline(v2) }';
179+
180+
// Less than
181+
static function instr_lt(v1: Instruction, v2: Instruction)
182+
return '${ callInline(v1) } < ${ callInline(v2) }';
183+
167184
static function instr_grouped_equation(v1: Instruction)
168185
return '(${ callInline(v1) })';
169186

@@ -199,6 +216,18 @@ class Instructions {
199216
static function instr_index_set(tbl: Instruction, key: Instruction, value: Instruction, type: Null<String>)
200217
return '${ callInline(tbl) }[${ callInline(key) }] = ${ callInline(value) }';
201218

219+
static function instr_kvtable(kvmap: Map<Instruction, Instruction>, imap: Array<Instruction>) {
220+
var kvargs = [ for (k => v in kvmap.keyValueIterator()) '[${ callInline(k) }] = ${ callInline(v) }' ];
221+
222+
return '{\n' +
223+
'\t${ kvargs.join(",\n\t") }\n' +
224+
'\t${ imap.map( (x) -> callInline(x) ).join(",\n\t") }\n' +
225+
'}';
226+
}
227+
228+
static function instr_kvarray()
229+
return 'todo!';
230+
202231
// Bitwise ops
203232
static function instr_bor(v1: Instruction, v2: Instruction) {
204233
#if LUA54

src/Lib/Std.hx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ final types: Map<String, E2TypeDef> = [
2626
}, // temporary.
2727
"vector2" => {
2828
id: "vector2"
29+
},
30+
"vector4" => {
31+
id: "vector4"
2932
}
3033
];
3134

0 commit comments

Comments
 (0)