Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
extremeheat committed Jun 5, 2021
1 parent 18f5c9e commit 853a5a5
Show file tree
Hide file tree
Showing 7 changed files with 2,547 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ProtoDef
44 changes: 44 additions & 0 deletions diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
diff --git a/src/compiler.js b/src/compiler.js
index 82856a2..82ed7b3 100644
--- a/src/compiler.js
+++ b/src/compiler.js
@@ -32,6 +32,12 @@ class ProtoDefCompiler {
this.sizeOfCompiler.addProtocol(protocolData, path)
}

+ addVariable (key, val) {
+ this.readCompiler.addContextType(key, val)
+ this.writeCompiler.addContextType(key, val)
+ this.sizeOfCompiler.addContextType(key, val)
+ }
+
compileProtoDefSync (options = { printCode: false }) {
const sizeOfCode = this.sizeOfCompiler.generate()
const writeCode = this.writeCompiler.generate()
@@ -70,6 +76,12 @@ class CompiledProtodef {
return writeFn(value, buffer, cursor)
}

+ setVariable (key, val) {
+ this.sizeOfCtx[key] = val
+ this.readCtx[key] = val
+ this.writeCtx[key] = val
+ }
+
sizeOf (value, type) {
const sizeFn = this.sizeOfCtx[type]
if (!sizeFn) { throw new Error('missing data type: ' + type) }
diff --git a/src/datatypes/compiler-conditional.js b/src/datatypes/compiler-conditional.js
index b817923..9d956be 100644
--- a/src/datatypes/compiler-conditional.js
+++ b/src/datatypes/compiler-conditional.js
@@ -10,7 +10,8 @@ module.exports = {
let code = `switch (${compare}) {\n`
for (const key in struct.fields) {
let val = key
- if (isNaN(val) && val !== 'true' && val !== 'false') val = `"${val}"`
+ if (val.startsWith('/')) val = 'ctx.' + val.slice(1) // Root context variable
+ else if (isNaN(val) && val !== 'true' && val !== 'false') val = `"${val}"`
code += compiler.indent(`case ${val}: return ` + compiler.callType(struct.fields[key])) + '\n'
}
code += compiler.indent('default: return ' + compiler.callType(struct.default ? struct.default : 'void')) + '\n'
58 changes: 58 additions & 0 deletions examples/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"main": [
"container",
[
{
"name": "packet",
"type": [
"switch",
{
"compareTo": "name",
"fields": {
"color_palette": "ColorPalette",
"draw_text": "DrawText"
},
"default": "void"
}
]
}
]
],
"ColorPalette": [
"container",
[
{
"name": "palette",
"type": [
"array",
{
"countType": "i32",
"type": "string"
}
]
}
]
],
"DrawText": [
"container",
[
{
"name": "color",
"type": "i32"
},
{
"name": "opacity",
"type": [
"switch",
{
"compareTo": "color",
"fields": {
"/color_transparent": "void"
},
"default": "u8"
}
]
}
]
]
}
13 changes: 13 additions & 0 deletions examples/sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
main:
packet: name ?
if color_palette: ColorPalette
if draw_text: DrawText

ColorPalette:
palette: string[]i32

DrawText:
color: i32
opacity: color ?
if /color_transparent: void
default: u8
Loading

0 comments on commit 853a5a5

Please sign in to comment.