Skip to content

Commit

Permalink
fixing paths
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Nov 30, 2022
1 parent 11b3d12 commit 9871c28
Show file tree
Hide file tree
Showing 15 changed files with 1,730 additions and 499 deletions.
2 changes: 1 addition & 1 deletion jacdac-ts
4 changes: 2 additions & 2 deletions src/components/blockly/dsl/dsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import jsep from "jsep"
import { Theme } from "@mui/material"
import Blockly, { Block, Workspace, WorkspaceSvg } from "blockly"
import { JDService } from "../../../../jacdac-ts/src/jdom/service"
import { RoleEvent } from "../../../../jacdac-ts/src/vm/compile"
import { VMError } from "../../../../jacdac-ts/src/vm/ir"
import { RoleEvent } from "../../devicescript/vm/compile"
import { VMError } from "../../devicescript/vm/ir"
import {
BlockDefinition,
ContentDefinition,
Expand Down
9 changes: 6 additions & 3 deletions src/components/blockly/dsl/loopsdsl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { toIdentifier } from "../../../../jacdac-ts/src/vm/compile"
import { VMCommand } from "../../../../jacdac-ts/src/vm/ir"
import { toIdentifier } from "../../devicescript/vm/compile"
import { VMCommand } from "../../devicescript/vm/ir"
import {
BlockReference,
CategoryDefinition,
Expand All @@ -8,7 +8,10 @@ import {
SeparatorDefinition,
ValueInputDefinition,
} from "../toolbox"
import { makeVMBase, processErrors } from "../../devicescript/JacscriptGenerator"
import {
makeVMBase,
processErrors,
} from "../../devicescript/JacscriptGenerator"
import BlockDomainSpecificLanguage from "./dsl"
import { paletteColorByIndex } from "./palette"

Expand Down
4 changes: 2 additions & 2 deletions src/components/blockly/dsl/servicesbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ import {
import {
toIdentifier,
toMemberExpression,
} from "../../../../jacdac-ts/src/vm/compile"
import { VMError } from "../../../../jacdac-ts/src/vm/ir"
} from "../../devicescript/vm/compile"
import { VMError } from "../../devicescript/vm/ir"
import NoteField from "../fields/NoteField"
import {
BlockDefinition,
Expand Down
2 changes: 1 addition & 1 deletion src/components/blockly/dsl/servicesdsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import jsep from "jsep"
import {
toIdentifier,
toMemberExpression,
} from "../../../../jacdac-ts/src/vm/compile"
} from "../../devicescript/vm/compile"
import { makeVMBase } from "../../devicescript/JacscriptGenerator"
import { arrayConcatMany, toMap } from "../../../../jacdac-ts/src/jdom/utils"

Expand Down
2 changes: 1 addition & 1 deletion src/components/blockly/dsl/variablesdsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import BlockDomainSpecificLanguage from "./dsl"
import {
toIdentifier,
toMemberExpression,
} from "../../../../jacdac-ts/src/vm/compile"
} from "../../devicescript/vm/compile"
import { makeVMBase } from "../../devicescript/JacscriptGenerator"

const variablesDsl: BlockDomainSpecificLanguage = {
Expand Down
4 changes: 2 additions & 2 deletions src/components/blockly/dsl/workspacejson.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { VMProgram } from "../../../../jacdac-ts/src/vm/ir"
import { JacscriptProgram } from "../../../../jacdac-ts/src/vm/ir2jacscript"
import { VMProgram } from "../../devicescript/vm/ir"
import { JacscriptProgram } from "../../devicescript/vm/ir2jacscript"
import { tidyHeaders } from "../fields/tidy"
import { BlockDataSet } from "../toolbox"

Expand Down
2 changes: 1 addition & 1 deletion src/components/devicescript/DeviceScriptVMDiagnostics.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Grid } from "@mui/material"
import React from "react"
import { VMProgram } from "../../../jacdac-ts/src/vm/ir"
import { VMProgram } from "../devicescript/vm/ir"
import CodeBlock from "../CodeBlock"

export default function DeviceScriptVMDiagnostics(props: { program: VMProgram }) {
Expand Down
5 changes: 2 additions & 3 deletions src/components/devicescript/JacscriptGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
VMRole,
VMIfThenElse,
VMError,
} from "../../../jacdac-ts/src/vm/ir"
import { RoleEvent, toIdentifier } from "../../../jacdac-ts/src/vm/compile"
} from "./vm/ir"
import { RoleEvent, toIdentifier } from "./vm/compile"
import {
BUILTIN_TYPES,
resolveBlockDefinition,
Expand All @@ -17,7 +17,6 @@ import Blockly from "blockly"
import BlockDomainSpecificLanguage, { resolveDsl } from "../blockly/dsl/dsl"
import { parseRoleType } from "../blockly/dsl/servicesbase"
import { BlockJSON, WorkspaceJSON } from "../blockly/dsl/workspacejson"
import { splitFilter } from "../../../jacdac-ts/src/jdom/utils"

export interface ExpressionWithErrors {
expr: jsep.Expression
Expand Down
237 changes: 237 additions & 0 deletions src/components/devicescript/vm/compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
import jsep from "jsep"
import {
VMBase,
VMCommand,
VMError,
VMHandler,
VMIfThenElse,
VMProgram,
VMFunctions,
} from "./ir"
import { VMChecker, SpecSymbolResolver } from "./symutils"
import { assert } from "../../../../jacdac-ts/src/jdom/utils"
import {
serviceSpecificationFromClassIdentifier,
serviceSpecificationFromName,
} from "../../../../jacdac-ts/src/jdom/spec"

export function toIdentifier(id: string) {
return {
type: "Identifier",
name: id,
} as jsep.Identifier
}

export function toMemberExpression(
root: string,
field: string | jsep.Expression
) {
return {
type: "MemberExpression",
object: toIdentifier(root),
property: typeof field === "string" ? toIdentifier(field) : field,
computed: false,
} as jsep.MemberExpression
}

function handlerVisitor(
handler: VMHandler,
visitITE: (ite: VMIfThenElse, time: number) => void,
visitCommand: (c: VMCommand) => void
) {
handler.commands.forEach(visitBase)

function visitBase(base: VMBase) {
switch (base.type) {
case "cmd": {
if (visitCommand) visitCommand(base as VMCommand)
break
}
case "ite": {
const ite = base as VMIfThenElse
if (visitITE) visitITE(ite, 0)
ite?.else?.forEach(visitBase)
if (visitITE) visitITE(ite, 1)
ite?.then?.forEach(visitBase)
if (visitITE) visitITE(ite, 2)
}
}
}
}

export function compileProgram({ roles, handlers }: VMProgram) {
const newProgram: VMProgram = { roles, handlers: [] }
// process start blocks
handlers.forEach(startBlock)
// remove if-then-else
newProgram.handlers = handlers.map(h => {
return { commands: removeIfThenElse(h), errors: h?.errors }
})
return newProgram
}

function checkCall(cmd: VMBase, id: string) {
if (cmd.type === "cmd") {
const callee = (cmd as VMCommand).command.callee
if (callee.type === "Identifier") {
const cid = (callee as jsep.Identifier).name
return id === cid
}
}
return undefined
}

function startBlock(handler: VMHandler) {
if (handler.commands.length && checkCall(handler.commands[0], "start")) {
handler.commands.shift()
handler.commands.push({
type: "cmd",
command: {
type: "CallExpression",
callee: toIdentifier("halt"),
arguments: [],
},
} as VMCommand)
}
}

function removeIfThenElse(handler: VMHandler): VMBase[] {
const newSequence: VMCommand[] = []
const labels: { then: string; end: string }[] = []
let labelId = 1
handlerVisitor(
handler,
(ite, time) => {
switch (time) {
case 0: {
// create the labels and branch instruction
const then = `then_${labelId}`
const end = `end_${labelId}`
labels.push({ then, end })
labelId++
newSequence.push({
type: "cmd",
command: {
type: "CallExpression",
callee: toIdentifier("branchOnCondition"),
arguments: [ite.expr, toIdentifier(then)],
},
})
break
}
case 1: {
// insert the jump and then label
const { then, end } = labels[labels.length - 1]
newSequence.push({
type: "cmd",
command: {
type: "CallExpression",
callee: toIdentifier("jump"),
arguments: [toIdentifier(end)],
},
})
newSequence.push({
type: "cmd",
command: {
type: "CallExpression",
callee: toIdentifier("label"),
arguments: [toIdentifier(then)],
},
})
break
}
case 2: {
assert(labels.length > 0)
const { end } = labels[labels.length - 1]
newSequence.push({
type: "cmd",
command: {
type: "CallExpression",
callee: toIdentifier("label"),
arguments: [toIdentifier(end)],
},
})
labels.pop()
}
}
},
cmd => {
newSequence.push(cmd)
}
)
return newSequence
}

export interface RoleRegister {
role: string
register: string
}

export interface RoleEvent {
role: string
event: string
}

export const getServiceFromRole = (info: VMProgram) => (role: string) => {
// lookup in roles first
const roleFound = info.roles.find(pair => pair.role === role)
if (roleFound) {
// must succeed
const spec = serviceSpecificationFromClassIdentifier(
roleFound.serviceClass
)
assert(!!spec, `service class ${roleFound.serviceClass} not resolved`)
return { spec }
} else {
const spec = serviceSpecificationFromName(role)
return { spec }
}
}

export function checkProgram(prog: VMProgram): {
registers: RoleRegister[]
events: RoleEvent[]
errors: VMError[]
} {
const allErrors: VMError[] = []
const goodHandlers: VMHandler[] = []
const errorFun = (e: string) => {
allErrors.push({ sourceId: undefined, message: e })
}
const symbolResolver = new SpecSymbolResolver(
undefined,
getServiceFromRole(prog),
errorFun
)
const checker = new VMChecker(symbolResolver, _ => true, errorFun)
prog.handlers.forEach(h => {
if (h?.errors.length) {
h?.errors.forEach(e => allErrors.push(e))
return
}
const errorCount = allErrors.length
symbolResolver.roles = []
handlerVisitor(h, undefined, c =>
checker.checkCommand(c.command, VMFunctions)
)
if (h?.errors.length === 0 && allErrors.length === errorCount) {
h.roles = symbolResolver.roles
goodHandlers.push(h)
} else {
h?.errors.forEach(e => allErrors.push(e))
}
})
prog.handlers = goodHandlers

return {
registers: symbolResolver.registers.map(s => {
const [root, fld] = s.split(".")
return { role: root, register: fld }
}),
events: symbolResolver.events.map(e => {
const [root, fld] = e.split(".")
return { role: root, event: fld }
}),
errors: allErrors,
}
}
Loading

0 comments on commit 9871c28

Please sign in to comment.