Skip to content

Commit

Permalink
Add context variables to compiler
Browse files Browse the repository at this point in the history
* new `addVariable` to ProtodefCompiler, `setVariable` to CompiledProtodef
* / at top of switch field will notate a context variable
  • Loading branch information
extremeheat committed May 24, 2021
1 parent 4431eef commit ee40de9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) }
Expand Down
9 changes: 6 additions & 3 deletions src/datatypes/compiler-conditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -39,7 +40,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('value', struct.fields[key])) + '\n'
}
code += compiler.indent('default: return ' + compiler.callType('value', struct.default ? struct.default : 'void')) + '\n'
Expand Down Expand Up @@ -69,7 +71,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('value', struct.fields[key])) + '\n'
}
code += compiler.indent('default: return ' + compiler.callType('value', struct.default ? struct.default : 'void')) + '\n'
Expand Down

0 comments on commit ee40de9

Please sign in to comment.