diff options
author | Alon Zakai <alonzakai@gmail.com> | 2011-04-18 11:37:24 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2011-04-18 11:37:24 -0700 |
commit | f89d9e2be63cbc3d4984dc748dac433dc3491fed (patch) | |
tree | 393919c2ae6b9c3deb725dd603ec72d9b5f4483d /src | |
parent | f72cf763607f2d69044493e9d1c3a818248ea929 (diff) |
refactor parsing of globals
Diffstat (limited to 'src')
-rw-r--r-- | src/intertyper.js | 57 | ||||
-rw-r--r-- | src/jsifier.js | 62 |
2 files changed, 72 insertions, 47 deletions
diff --git a/src/intertyper.js b/src/intertyper.js index efeba90a..db94b8af 100644 --- a/src/intertyper.js +++ b/src/intertyper.js @@ -310,6 +310,58 @@ function intertyper(data, parseFunctions, baseLineNum) { // globals: type or variable substrate.addActor('Global', { processItem: function(item) { + function scanConst(value, type) { + //dprint('inter-const: ' + item.lineNum + ' : ' + JSON.stringify(value) + ',' + type + '\n'); + if (Runtime.isNumberType(type) || pointingLevels(type) >= 1) { + return { value: toNiceIdent(value.text), type: type }; + } else if (value.text in set('zeroinitializer', 'undef')) { // undef doesn't really need initting, but why not + return { intertype: 'emptystruct', type: type }; + } else if (value.text && value.text[0] == '"') { + return { intertype: 'string', text: value.text.substr(1, value.text.length-2) }; + } else { + // Gets an array of constant items, separated by ',' tokens + function handleSegments(tokens) { + // Handle a single segment (after comma separation) + function handleSegment(segment) { + if (segment[1].text == 'null') { + return { intertype: 'value', value: 0 }; + } else if (segment[1].text == 'zeroinitializer') { + return { intertype: 'emptystruct', type: segment[0].text }; + } else if (segment[1].text in PARSABLE_LLVM_FUNCTIONS) { + return parseLLVMFunctionCall(segment); + } else if (segment[1].type == '{') { + return { intertype: 'struct', type: segment[0].text, contents: handleSegments(segment[1].tokens) }; + } else if (segment[1].type == '[') { + return { intertype: 'list', type: segment[0].text, contents: handleSegments(segment[1].item.tokens) }; + } else if (segment.length == 2) { + return { intertype: 'value', value: toNiceIdent(segment[1].text) }; + } else if (segment[1].text === 'c') { + // string + var text = segment[2].text; + text = text.substr(1, text.length-2); + return { intertype: 'string', text: text }; + } else { + throw 'Invalid segment: ' + dump(segment); + } + }; + return splitTokenList(tokens).map(handleSegment); + } + var contents; + if (value.item) { + // list of items + contents = value.item.tokens; + } else if (value.type == '{') { + // struct + contents = value.tokens; + } else if (value[0]) { + contents = value[0]; + } else { + throw '// interfailzzzzzzzzzzzzzz ' + dump(value.item) + ' ::: ' + dump(value); + } + return { intertype: 'segments', contents: handleSegments(contents) }; + } + } + if (item.tokens[2].text == 'alias') { return; // TODO: handle this. See raytrace.cpp } @@ -374,9 +426,10 @@ function intertyper(data, parseFunctions, baseLineNum) { if (item.tokens[3].text == 'c') item.tokens.splice(3, 1); - ret.value = item.tokens[3]; - if (ret.value.text in PARSABLE_LLVM_FUNCTIONS) { + if (item.tokens[3].text in PARSABLE_LLVM_FUNCTIONS) { ret.value = parseLLVMFunctionCall(item.tokens.slice(2)); + } else if (!external) { + ret.value = scanConst(item.tokens[3], ret.type); } } return [ret]; diff --git a/src/jsifier.js b/src/jsifier.js index 8e469c2b..f185cbd2 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -95,66 +95,38 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { // Gets an entire constant expression function makeConst(value, type, ident) { - //dprint('gconst', '//yyyyy ' + JSON.stringify(value) + ',' + type + '\n'); - if (value.intertype) { + //dprint('jsifier const: ' + JSON.stringify(value) + ',' + type + '\n'); + if (value.intertype in PARSABLE_LLVM_FUNCTIONS) { return [finalizeLLVMFunctionCall(value)]; } else if (Runtime.isNumberType(type) || pointingLevels(type) >= 1) { - return indexizeFunctions(parseNumerical(toNiceIdent(value.text))); - } else if (value.text in set('zeroinitializer', 'undef')) { // undef doesn't really need initting, but why not + return indexizeFunctions(parseNumerical(value.value)); + } else if (value.intertype === 'emptystruct') { return makeEmptyStruct(type); - } else if (value.text && value.text[0] == '"') { - value.text = value.text.substr(1, value.text.length-2); + } else if (value.intertype === 'string') { return JSON.stringify(parseLLVMString(value.text)) + ' /* ' + value.text.substr(0, 20).replace(/\*/g, '_') + ' */'; // make string safe for inclusion in comment } else { // Gets an array of constant items, separated by ',' tokens function handleSegments(tokens) { - //dprint('gconst', '// segggS: ' + JSON.stringify(tokens) + '\n' + '\n') // Handle a single segment (after comma separation) function handleSegment(segment) { - //dprint('// seggg: ' + JSON.stringify(segment) + '\n' + '\n') - if (segment[1].text == 'null') { - return '0'; - } else if (segment[1].text == 'zeroinitializer') { - return makeEmptyStruct(segment[0].text); - } else if (segment[1].text in set('bitcast', 'inttoptr', 'ptrtoint')) { // TODO: Use parse/finalizeLLVMFunctionCall - var type = segment[2].item.tokens.slice(-1)[0].text; // TODO: Use this? - return handleSegment(segment[2].item.tokens.slice(0, -2)); - } else if (segment[1].text in PARSABLE_LLVM_FUNCTIONS) { - return finalizeLLVMFunctionCall(parseLLVMFunctionCall(segment)); - } else if (segment[1].type == '{') { - // struct - var type = segment[0].text; - return alignStruct(handleSegments(segment[1].tokens), type); - } else if (segment[1].type == '[') { - var type = segment[0].text; - return alignStruct(handleSegments(segment[1].item.tokens), type); - } else if (segment.length == 2) { - return toNiceIdent(segment[1].text); - } else if (segment[1].text === 'c') { - // string - var text = segment[2].text; - text = text.substr(1, text.length-2); - return parseLLVMString(text); // + ' /* ' + text + '*/'; + if (segment.intertype === 'value') { + return segment.value.toString(); + } else if (segment.intertype === 'emptystruct') { + return makeEmptyStruct(segment.type); + } else if (segment.intertype in PARSABLE_LLVM_FUNCTIONS) { + return finalizeLLVMFunctionCall(segment); + } else if (segment.intertype in set('struct', 'list')) { + return alignStruct(handleSegments(segment.contents), segment.type); + } else if (segment.intertype === 'string') { + return parseLLVMString(segment.text); // + ' /* ' + text + '*/'; } else { throw 'Invalid segment: ' + dump(segment); } }; - return splitTokenList(tokens).map(handleSegment).map(indexizeFunctions); + return tokens.map(handleSegment).map(indexizeFunctions); } - var contents; - if (value.item) { - // list of items - contents = value.item.tokens; - } else if (value.type == '{') { - // struct - contents = value.tokens; - } else if (value[0]) { - contents = value[0]; - } else { - throw '// failzzzzzzzzzzzzzz ' + dump(value.item) + ' ::: ' + dump(value); - } - return alignStruct(handleSegments(contents), type); + return alignStruct(handleSegments(value.contents), type); } } |