-
Notifications
You must be signed in to change notification settings - Fork 3
Handle Int
nodes.
#1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,6 +163,8 @@ export class Frontend { | |
} else if (node instanceof source.node.Consume) { | ||
result = new nodeImpl.Consume( | ||
new frontend.node.Consume(id(), node.field)); | ||
} else if (node instanceof source.node.Int) { | ||
result = this.translateInt(node); | ||
} else if (node instanceof source.node.SpanStart) { | ||
result = new nodeImpl.SpanStart( | ||
new frontend.node.SpanStart(id(), this.spanMap.get(node.span)!, | ||
|
@@ -184,27 +186,37 @@ export class Frontend { | |
|
||
// Initialize result | ||
const otherwise = node.getOtherwiseEdge(); | ||
|
||
if (Array.isArray(result)) { | ||
assert(node instanceof source.node.Match); | ||
const match = node as source.node.Match; | ||
assert(node instanceof source.node.Match || node instanceof source.node.Int); | ||
|
||
// TODO(indutny): move this to llparse-builder? | ||
assert.notStrictEqual(otherwise, undefined, | ||
`Node "${node.name}" has no \`.otherwise()\``); | ||
if (node instanceof source.node.Match) { | ||
// TODO(indutny): move this to llparse-builder? | ||
assert.notStrictEqual(otherwise, undefined, | ||
`Node "${node.name}" has no \`.otherwise()\``); | ||
|
||
// Assign otherwise to every node of Trie | ||
if (otherwise !== undefined) { | ||
// Assign otherwise to every node of Trie | ||
if (otherwise !== undefined) { | ||
for (const child of result) { | ||
child.ref.setOtherwise(this.translate(otherwise.node), | ||
otherwise.noAdvance); | ||
} | ||
} | ||
|
||
// Assign transform to every node of Trie | ||
const transform = this.translateTransform(node.getTransform()); | ||
for (const child of result) { | ||
child.ref.setOtherwise(this.translate(otherwise.node), | ||
otherwise.noAdvance); | ||
child.ref.setTransform(transform); | ||
} | ||
} | ||
} else if (node instanceof source.node.Int) { | ||
// TODO(indutny): move this to llparse-builder? | ||
assert.notStrictEqual(otherwise, undefined, | ||
`Node "${node.name}" has no \`.otherwise()\``); | ||
|
||
// Assign transform to every node of Trie | ||
const transform = this.translateTransform(match.getTransform()); | ||
for (const child of result) { | ||
child.ref.setTransform(transform); | ||
// Assign otherwise to last node of int expansion | ||
if (otherwise !== undefined) { | ||
result[result.length - 1].ref.setOtherwise(this.translate(otherwise.node), | ||
otherwise.noAdvance) | ||
} | ||
} | ||
|
||
assert(result.length >= 1); | ||
|
@@ -256,6 +268,31 @@ export class Frontend { | |
} | ||
} | ||
|
||
private translateInt(node: source.node.Int) : ReadonlyArray<IWrap<frontend.node.Node>> { | ||
const { name, field, bytes, signed, littleEndian } = node; | ||
|
||
let result = [ | ||
new this.implementation.node.Int( | ||
new frontend.node.Int(this.id.id(name), field, bytes, signed, littleEndian, 0) | ||
) | ||
]; | ||
|
||
// Break loops | ||
this.map.set(node, result[0]); | ||
|
||
let next = result[0]; | ||
for (let offset = 1; offset < node.bytes; offset++) { | ||
result[offset] = new this.implementation.node.Int( | ||
new frontend.node.Int(this.id.id(name + '_byte' + (offset + 1)), field, bytes, signed, littleEndian, offset) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: similar recommendation here. Perhaps even move |
||
); | ||
|
||
next.ref.setOtherwise(result[offset], false); | ||
next = result[offset]; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private translateMatch(node: source.node.Match): MatchResult { | ||
const trie = new Trie(node.name); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { IUniqueName } from '../utils'; | ||
import { Node } from './base'; | ||
|
||
export class Int extends Node { | ||
constructor(id: IUniqueName, readonly field: string, public readonly bytes: number, public readonly signed: boolean, public readonly littleEndian: boolean, public readonly byteOffset: number) { | ||
super(id); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: Let's split it into multiple statements. Something like:
const tmp = new frontend.node.Int(...)
.