Skip to content

Commit

Permalink
Merge branch 'refactor-global-code' of github.com:sebpiq/WebPd_compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
sebpiq committed Aug 29, 2024
2 parents d450d0d + 7a0d196 commit cb763bc
Show file tree
Hide file tree
Showing 106 changed files with 6,406 additions and 4,730 deletions.
68 changes: 43 additions & 25 deletions src/ast/ast-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,64 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import assert from "assert"
import { assertFuncSignatureEqual } from "./ast-helpers"
import { AnonFunc, Var } from "./declare"
import assert from 'assert'
import { assertFuncSignatureEqual } from './ast-helpers'
import { AnonFunc, Var } from './declare'

describe('ast-helpers', () => {
describe('assertFuncSignatureEqual', () => {
it('should throw if actual is not an ast Func', () => {
assert.throws(() => assertFuncSignatureEqual(1 as any, AnonFunc()``))
assert.throws(() =>
assertFuncSignatureEqual(1 as any, AnonFunc()``)
)
})

it('should throw if functions dont have the same arguments type or count', () => {
assert.throws(() => assertFuncSignatureEqual(
AnonFunc([Var('Int', 'bla')])``,
AnonFunc([Var('Float', 'bla')])``
))
assert.throws(() => assertFuncSignatureEqual(
AnonFunc([Var('Int', 'bla'), Var('Int', 'blo')])``,
AnonFunc([Var('Int', 'bla')])``
))
assert.throws(() =>
assertFuncSignatureEqual(
AnonFunc([Var(`Int`, `bla`)])``,
AnonFunc([Var(`Float`, `bla`)])``
)
)
assert.throws(() =>
assertFuncSignatureEqual(
AnonFunc([Var(`Int`, `bla`), Var(`Int`, `blo`)])``,
AnonFunc([Var(`Int`, `bla`)])``
)
)
})

it('should throw if functions dont have the same return type', () => {
assert.throws(() => assertFuncSignatureEqual(
AnonFunc([Var('Int', 'bla')], 'Int')``,
AnonFunc([Var('Int', 'bla')], 'Float')``
))
assert.throws(() =>
assertFuncSignatureEqual(
AnonFunc([Var(`Int`, `bla`)], `Int`)``,
AnonFunc([Var(`Int`, `bla`)], `Float`)``
)
)
})

it('should not throw if functions have different argument names', () => {
assert.doesNotThrow(() => assertFuncSignatureEqual(
AnonFunc([Var('Int', 'bla')])``,
AnonFunc([Var('Int', 'blo')])``
))
assert.doesNotThrow(() =>
assertFuncSignatureEqual(
AnonFunc([Var(`Int`, `bla`)])``,
AnonFunc([Var(`Int`, `blo`)])``
)
)
})

it('should not throw if functions have the same signature', () => {
assert.doesNotThrow(() => assertFuncSignatureEqual(
AnonFunc([Var('Int', 'bla'), Var('Array<Bla>', 'blo')], 'string')``,
AnonFunc([Var('Int', 'bla'), Var('Array<Bla>', 'blo')], 'string')``
))
assert.doesNotThrow(() =>
assertFuncSignatureEqual(
AnonFunc(
[Var(`Int`, `bla`), Var(`Array<Bla>`, `blo`)],
'string'
)``,
AnonFunc(
[Var(`Int`, `bla`), Var(`Array<Bla>`, `blo`)],
'string'
)``
)
)
})
})
})
})
38 changes: 19 additions & 19 deletions src/ast/declare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ import { AstSequence, AstFunc } from './types'
describe('declare', () => {
describe('_processRawContent', () => {
it('should filter out null', () => {
const someVar = Var('Int', 'bla')
const someVar = Var(`Int`, `bla`)
assert.deepStrictEqual(
_processRawContent([null, 'a', someVar, null, 'b', null]),
['a', someVar, 'b']
)
})

it('should combine adjacent strings', () => {
const someVar = Var('Int', 'bla')
const someVar = Var(`Int`, `bla`)
assert.deepStrictEqual(
_processRawContent(['a', 'b', someVar, 'c', 'd']),
['ab', someVar, 'cd']
Expand All @@ -55,9 +55,9 @@ describe('declare', () => {
})

it('should recursively flatten arrays, add newlines between array elements and recusively combine strings', () => {
const var1 = Var('Int', 'bla')
const var2 = Var('Int', 'blu')
const var3 = Var('Int', 'bli')
const var1 = Var(`Int`, `bla`)
const var2 = Var(`Int`, `blu`)
const var3 = Var(`Int`, `bli`)
assert.deepStrictEqual(
_processRawContent([
['a', 'b', var1],
Expand All @@ -70,14 +70,14 @@ describe('declare', () => {
})

it('should expand AstSequence', () => {
const var1 = Var('Int', 'bla')
const var2 = Var('Int', 'blu')
const var1 = Var(`Int`, `bla`)
const var2 = Var(`Int`, `blu`)
const ast1: AstSequence = {
astType: 'Sequence',
content: [var1, 'blo', var2],
}

const var3 = Var('Int', 'bli')
const var3 = Var(`Int`, `bli`)
const ast2: AstSequence = {
astType: 'Sequence',
content: [var3, 'bly'],
Expand All @@ -89,14 +89,14 @@ describe('declare', () => {
})

it('should leave other AST elements untouched', () => {
const var1 = Var('Int', 'bla')
const var2 = Var('Int', 'blu')
const var1 = Var(`Int`, `bla`)
const var2 = Var(`Int`, `blu`)
const func1 = Func('myFunc')`
${var1}
${var2}
`

const var3 = Var('Int', 'bli')
const var3 = Var(`Int`, `bli`)
const class1 = Class('myClass', [var3])

assert.deepStrictEqual(
Expand All @@ -108,7 +108,7 @@ describe('declare', () => {

describe('Sequence', () => {
it('should intersperse newlines between elements', () => {
const var1 = Var('Int', 'bla')
const var1 = Var(`Int`, `bla`)
const sequence = Sequence(['a', 'b', var1, 'c'])
assert.deepStrictEqual<AstSequence>(sequence, {
astType: 'Sequence',
Expand All @@ -119,8 +119,8 @@ describe('declare', () => {

describe('Ast', () => {
it('should intersperse newlines between array elements, but not mess with top-level strings', () => {
const var1 = Var('Int', 'bla', '1')
const var2 = ConstVar('Int', 'blu', '3')
const var1 = Var(`Int`, `bla`, `1`)
const var2 = ConstVar(`Int`, `blu`, `3`)
const sequence = ast`
${var1}
bla = 2
Expand All @@ -141,7 +141,7 @@ describe('declare', () => {
it('should allow to declare a function', () => {
const astFunc = Func(
'myFunc',
[Var('number', 'arg1'), Var('string', 'arg2')],
[Var(`number`, `arg1`), Var(`string`, `arg2`)],
'void'
)`const a = 1`

Expand Down Expand Up @@ -172,8 +172,8 @@ describe('declare', () => {

it('should allow function body to declare variables', () => {
const astFunc = Func('myFunc', [], 'string')`
${Var('number', 'a', '1')}
${ConstVar('string', 'b', '"HELLO"')}
${Var(`number`, `a`, `1`)}
${ConstVar(`string`, `b`, `"HELLO"`)}
return b`

assert.deepStrictEqual<AstFunc>(astFunc, {
Expand Down Expand Up @@ -261,7 +261,7 @@ describe('declare', () => {
describe('AnonFunc', () => {
it('should allow to declare an anonymous function', () => {
const astFunc = AnonFunc(
[Var('number', 'arg1')],
[Var(`number`, `arg1`)],
'void'
)`const a = 1`

Expand Down Expand Up @@ -302,7 +302,7 @@ describe('declare', () => {

describe('Var', () => {
it('should accept value as number', () => {
const var1 = Var('Int', 'bla', 1)
const var1 = Var(`Int`, `bla`, 1)
assert.deepStrictEqual(var1, {
astType: 'Var',
name: 'bla',
Expand Down
16 changes: 7 additions & 9 deletions src/ast/declare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,17 @@ export const _processRawContent = (
*/
const _intersperse = (
array1: Readonly<AstContentRawNested>,
array2: Readonly<AstContentRawNested>,
array2: Readonly<AstContentRawNested>
): AstContentRawNested => {
if (array1.length === 0) {
return []
}
return array1
.slice(1)
.reduce<AstContentRawNested>(
(combinedContent, element, i) => {
return combinedContent.concat([array2[i]!, element])
},
[array1[0]!]
)
return array1.slice(1).reduce<AstContentRawNested>(
(combinedContent, element, i) => {
return combinedContent.concat([array2[i]!, element])
},
[array1[0]!]
)
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/ast/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* Code string, either provided as part of an AST,
* or the result of a compilation operation
/**
* Code string, either provided as part of an AST,
* or the result of a compilation operation
*/
export type Code = string

Expand Down Expand Up @@ -77,4 +77,3 @@ export interface AstClass extends AstElementBase {
name: VariableName
members: Array<AstVar>
}

3 changes: 1 addition & 2 deletions src/compile/compile-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ describe('compile-helpers', () => {

it('should return node implementation if it exists', () => {
assert.strictEqual(
getNodeImplementation(NODE_IMPLEMENTATIONS, 'someNodeType')
.dsp,
getNodeImplementation(NODE_IMPLEMENTATIONS, 'someNodeType').dsp,
NODE_IMPLEMENTATIONS.someNodeType!.dsp!
)
})
Expand Down
7 changes: 0 additions & 7 deletions src/compile/compile-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import jsMacros from '../engine-javascript/compile/macros'
import ascMacros from '../engine-assemblyscript/compile/macros'
import {
CompilerTarget,
GlobalCodeDefinition,
GlobalCodeGeneratorWithSettings,
NodeImplementation,
NodeImplementations,
} from './types'
Expand Down Expand Up @@ -102,8 +100,3 @@ export const buildGraphTraversalSignal = (

export const getGraphSignalSinks = (graph: DspGraph.Graph) =>
Object.values(graph).filter((node) => !!node.isPullingSignal)

export const isGlobalDefinitionWithSettings = (
globalCodeDefinition: GlobalCodeDefinition
): globalCodeDefinition is GlobalCodeGeneratorWithSettings =>
!(typeof globalCodeDefinition === 'function')
14 changes: 12 additions & 2 deletions src/compile/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,23 @@ describe('compile', () => {
}

it('should compile assemblyscript without error', () => {
const compileResult = compile({}, {}, 'assemblyscript', COMPILER_SETTINGS_AS)
const compileResult = compile(
{},
{},
'assemblyscript',
COMPILER_SETTINGS_AS
)
assert.ok(compileResult.status === 0)
assert.strictEqual(typeof compileResult.code, 'string')
})

it('should compile javascript without error', () => {
const compileResult = compile({}, {}, 'javascript', COMPILER_SETTINGS_JS)
const compileResult = compile(
{},
{},
'javascript',
COMPILER_SETTINGS_JS
)
assert.ok(compileResult.status === 0)
assert.strictEqual(typeof compileResult.code, 'string')
})
Expand Down
31 changes: 14 additions & 17 deletions src/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { AssemblyScriptWasmEngineCode } from '../engine-assemblyscript/compile/t
import { DspGraph } from '../dsp-graph/types'
import precompile from './precompile'
import { validateSettings } from './settings'
import { RenderInput } from './render/types'
import { proxyAsReadOnlyIndex } from './proxies'

interface CompilationSuccess {
status: 0
Expand All @@ -49,27 +51,22 @@ export default (
compilationSettings: UserCompilationSettings
): CompilationResult => {
const settings = validateSettings(compilationSettings, target)
const { precompiledCode, variableNamesIndex } = precompile(
{
graph,
nodeImplementations,
settings,
}
)
const { precompiledCode, variableNamesIndex } = precompile({
graph,
nodeImplementations,
settings,
})

let code: JavaScriptEngineCode | AssemblyScriptWasmEngineCode
const renderInput: RenderInput = {
precompiledCode,
settings,
variableNamesReadOnly: proxyAsReadOnlyIndex(variableNamesIndex),
}
if (target === 'javascript') {
code = renderToJavascript({
precompiledCode,
settings,
variableNamesIndex,
})
code = renderToJavascript(renderInput)
} else if (target === 'assemblyscript') {
code = renderToAssemblyscript({
precompiledCode,
settings,
variableNamesIndex,
})
code = renderToAssemblyscript(renderInput)
} else {
throw new Error(`Invalid target ${target}`)
}
Expand Down
Loading

0 comments on commit cb763bc

Please sign in to comment.