Just realized point { x = 1 } was ambiguous, could be record update or function application. changed record update syntax to: point.{ x = 1 }. And starting on ui components
This commit is contained in:
parent
645de97db2
commit
d55ae33848
4 changed files with 32 additions and 14 deletions
|
|
@ -7,6 +7,7 @@ import { builtins } from './builtins';
|
||||||
|
|
||||||
import counterApp from './counter.cg?raw';
|
import counterApp from './counter.cg?raw';
|
||||||
import stdlibCode from './stdlib.cg?raw';
|
import stdlibCode from './stdlib.cg?raw';
|
||||||
|
import uiComponentsCode from './ui-components.cg?raw';
|
||||||
import textInputCode from './textinput-test.cg?raw';
|
import textInputCode from './textinput-test.cg?raw';
|
||||||
import testCode from './test.cg?raw';
|
import testCode from './test.cg?raw';
|
||||||
|
|
||||||
|
|
@ -15,7 +16,7 @@ canvas.width = 800;
|
||||||
canvas.height = 600;
|
canvas.height = 600;
|
||||||
document.body.appendChild(canvas);
|
document.body.appendChild(canvas);
|
||||||
|
|
||||||
const cgCode = stdlibCode + '\n' + textInputCode;
|
const cgCode = stdlibCode + '\n' + uiComponentsCode + '\n' + textInputCode;
|
||||||
|
|
||||||
const tokens = tokenize(cgCode);
|
const tokens = tokenize(cgCode);
|
||||||
const parser = new Parser(tokens);
|
const parser = new Parser(tokens);
|
||||||
|
|
|
||||||
|
|
@ -319,17 +319,9 @@ export class Parser {
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (this.current().kind === 'dot') {
|
if (this.current().kind === 'dot') {
|
||||||
// Record access
|
|
||||||
this.advance();
|
this.advance();
|
||||||
const fieldToken = this.expect('ident');
|
|
||||||
const field = (fieldToken as { value: string }).value;
|
if (this.current().kind === 'open-brace') {
|
||||||
expr = { kind: 'record-access', record: expr, field };
|
|
||||||
} else if (this.current().kind === 'open-brace') {
|
|
||||||
if (expr.kind === 'constructor') {
|
|
||||||
// Constructor application
|
|
||||||
const record = this.parsePrimary();
|
|
||||||
expr = { kind: 'apply', func: expr, args: [record] };
|
|
||||||
} else {
|
|
||||||
// Record update
|
// Record update
|
||||||
this.advance();
|
this.advance();
|
||||||
const updates: { [key: string]: AST } = {};
|
const updates: { [key: string]: AST } = {};
|
||||||
|
|
@ -349,7 +341,21 @@ export class Parser {
|
||||||
|
|
||||||
this.expect('close-brace');
|
this.expect('close-brace');
|
||||||
expr = { kind: 'record-update', record: expr, updates }
|
expr = { kind: 'record-update', record: expr, updates }
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Record access
|
||||||
|
const fieldToken = this.expect('ident');
|
||||||
|
const field = (fieldToken as { value: string }).value;
|
||||||
|
expr = { kind: 'record-access', record: expr, field };
|
||||||
}
|
}
|
||||||
|
} else if (this.current().kind === 'open-brace') {
|
||||||
|
// if (expr.kind === 'constructor') {
|
||||||
|
// Function / constructor application
|
||||||
|
const record = this.parsePrimary();
|
||||||
|
expr = { kind: 'apply', func: expr, args: [record] };
|
||||||
|
// } else {
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,17 @@ export function runApp(app: App, canvas: HTMLCanvasElement) {
|
||||||
if (app.view.kind !== 'closure')
|
if (app.view.kind !== 'closure')
|
||||||
throw new Error('view must be a function');
|
throw new Error('view must be a function');
|
||||||
|
|
||||||
|
const viewport = {
|
||||||
|
kind: 'record',
|
||||||
|
fields: {
|
||||||
|
width: { kind: 'int', value: canvas.width },
|
||||||
|
height: { kind: 'int', value: canvas.height }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const callEnv = new Map(app.view.env);
|
const callEnv = new Map(app.view.env);
|
||||||
callEnv.set(app.view.params[0], state);
|
callEnv.set(app.view.params[0], state);
|
||||||
|
callEnv.set(app.view.params[1], viewport);
|
||||||
const uiValue = evaluate(app.view.body, callEnv);
|
const uiValue = evaluate(app.view.body, callEnv);
|
||||||
const ui = valueToUI(uiValue);
|
const ui = valueToUI(uiValue);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
init = { text = "" };
|
init = { text = "" };
|
||||||
|
|
||||||
update = state event \ event
|
update = state event \ event
|
||||||
| UpdateText newText \ state { text = newText }
|
| UpdateText newText \ state.{ text = newText }
|
||||||
| Submit _ \ state { text = "" };
|
| Submit _ \ state.{ text = "" };
|
||||||
|
# | _ \ state;
|
||||||
|
|
||||||
view = state \
|
view = state viewport \
|
||||||
Column {
|
Column {
|
||||||
gap = 20,
|
gap = 20,
|
||||||
children = [
|
children = [
|
||||||
|
|
@ -23,6 +24,7 @@ view = state \
|
||||||
onInput = UpdateText,
|
onInput = UpdateText,
|
||||||
onSubmit = Submit
|
onSubmit = Submit
|
||||||
}
|
}
|
||||||
|
# button ({ label = "Go", event = Go })
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue