walt-compiler
Advanced tools
Comparing version 0.15.0 to 0.15.1
{ | ||
"name": "walt-compiler", | ||
"version": "0.15.0", | ||
"version": "0.15.1", | ||
"description": "Alternative syntax for WebAssembly text format", | ||
@@ -5,0 +5,0 @@ "main": "dist/walt.js", |
@@ -9,3 +9,3 @@ /** | ||
import OutputStream from '../utils/output-stream'; | ||
import { i32 } from 'wasm-types'; | ||
import wasmTypes from 'wasm-types'; | ||
import type { SemanticPlugin } from '../flow/types'; | ||
@@ -32,6 +32,7 @@ | ||
function encodeArray(array) { | ||
function encodeArray(array, type) { | ||
const stream = new OutputStream(); | ||
const encodeType = wasmTypes[type]; | ||
array.forEach(v => { | ||
stream.push(i32, v, String(v)); | ||
stream.push(encodeType, v, String(v)); | ||
}); | ||
@@ -48,4 +49,6 @@ | ||
const { userTypes, statics } = context; | ||
const bareType = String(node.type).slice(0, -2); | ||
const typeSize = sizeMap[bareType] || 4; | ||
const typeSize = sizeMap[bareType]; | ||
const meta = node.params.reduce( | ||
@@ -79,3 +82,3 @@ (acc, v, i) => { | ||
statics[uid] = encodeArray(meta.VALUES); | ||
statics[uid] = encodeArray(meta.VALUES, bareType); | ||
@@ -82,0 +85,0 @@ // Short circuit the middleware and instead transform a declaration |
@@ -40,3 +40,3 @@ // @flow | ||
encodeNames = false, | ||
lines = source ? source.split('\n') : [], | ||
lines = source.split('\n'), | ||
filename = 'unknown', | ||
@@ -91,12 +91,12 @@ extensions = [], | ||
(acc, plugin) => { | ||
const instance = plugin(options); | ||
// Default plugins to a specific to ensure correctness | ||
const instance = { | ||
semantics: _ => ({}), | ||
grammar: () => ({ ParserRules: [] }), | ||
...plugin(options), | ||
}; | ||
if (typeof instance.grammar === 'function') { | ||
acc.grammar.push(instance.grammar); | ||
} | ||
acc.grammar.push(instance.grammar); | ||
acc.semantics.push(instance.semantics); | ||
if (typeof instance.semantics === 'function') { | ||
acc.semantics.push(instance.semantics); | ||
} | ||
return acc; | ||
@@ -103,0 +103,0 @@ }, |
import test from 'ava'; | ||
import compose from '../../utils/compose'; | ||
import { debug, getIR, compile } from '../..'; | ||
import { compile } from '../..'; | ||
@@ -29,9 +28,9 @@ const walt = `export function _32BitSizes(): i32 { | ||
} | ||
export function nativeTypes(): i32 { | ||
return sizeof(i32) / sizeof(f32); | ||
} | ||
`; | ||
test('type sizes', t => { | ||
const getWasm = compose(debug, getIR); | ||
const wasm = getWasm(walt); | ||
t.snapshot(wasm); | ||
return WebAssembly.instantiate(compile(walt).buffer()).then(result => { | ||
@@ -42,3 +41,4 @@ t.is(result.instance.exports._32BitSizes(), 8, '32 bit sizes combined'); | ||
t.is(result.instance.exports.userDefinedTypeName(), 16, 'type-name'); | ||
t.is(result.instance.exports.nativeTypes(), 1, 'native type sizes'); | ||
}); | ||
}); |
@@ -28,5 +28,12 @@ /** | ||
/** | ||
* Returns a custom lexer. This wrapper API is necessary to ignore comments | ||
* in all of the subsequent compiler phases, unfortunately. | ||
* | ||
* TODO: Maybe consider adding comment nodes back to the AST. IIRC this causes | ||
* lots of ambiguous grammar for whatever reason. | ||
*/ | ||
function makeLexer() { | ||
const mooLexer = moo.compile(tokens); | ||
// Additional utility on top of the default moo lexer. | ||
return { | ||
@@ -51,2 +58,3 @@ current: null, | ||
let token = mooLexer.next(); | ||
// Drop all comment tokens found | ||
while (token && token.type === 'comment') { | ||
@@ -79,2 +87,4 @@ token = mooLexer.next(); | ||
// All Grammar plugins are factories resulting in an object which must contain | ||
// a "ParserRules" array which will be added to the base grammar. | ||
const grammar = grammarList.slice(1).reduce((acc: any, value: Function) => { | ||
@@ -92,2 +102,5 @@ const extra = value.call(context); | ||
// This is a safeguard against ambiguous syntax that may be generated by blending | ||
// multiple different grammars together. If there is more than one was to parse | ||
// something then we did something wrong and we hard exit the compiler pipeline. | ||
invariant( | ||
@@ -94,0 +107,0 @@ parser.results.length === 1, |
@@ -130,8 +130,8 @@ import { makeFragment } from '../../parser/fragment'; | ||
return { | ||
semantics(...args) { | ||
calls.push(['semantics', args]); | ||
semantics(..._args) { | ||
calls.push(['semantics', _args]); | ||
return {}; | ||
}, | ||
grammar(...args) { | ||
calls.push(['grammar', args]); | ||
grammar(..._args) { | ||
calls.push(['grammar', _args]); | ||
return { | ||
@@ -143,6 +143,7 @@ ParserRules: {}, | ||
}; | ||
const plugin2 = () => ({}); | ||
compile('const x: i32 = 0;', { extensions: [plugin] }); | ||
compile('const x: i32 = 0;', { extensions: [plugin, plugin2] }); | ||
t.snapshot(calls); | ||
}); |
@@ -28,3 +28,3 @@ /** | ||
import memory from '../core/memory'; | ||
import statics from '../core/statics'; | ||
import _statics from '../core/statics'; | ||
import functionPointer from '../core/function-pointer'; | ||
@@ -53,3 +53,3 @@ import struct from '../core/struct'; | ||
memory, | ||
statics, | ||
_statics, | ||
functionPointer, | ||
@@ -73,3 +73,3 @@ struct, | ||
memory().semantics, | ||
statics().semantics, | ||
_statics().semantics, | ||
functionPointer().semantics, | ||
@@ -86,3 +86,3 @@ struct().semantics, | ||
ast: NodeType, | ||
extraSemantics: SemanticsFactory[] = [], | ||
extraSemantics: SemanticsFactory[], | ||
options: SemanticOptions | ||
@@ -89,0 +89,0 @@ ): NodeType { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
10392463
327
12495