namespacing the store functions. allowing _ in params

master
Dustin Swan 1 day ago
parent 1af838126e
commit 9b8916eb72
Signed by: dustinswan
GPG Key ID: 30D46587E2100467

@ -36,7 +36,7 @@ function expectString(v: Value, name: string): string {
// return v.elements; // return v.elements;
// } // }
export const builtins: { [name: string]: NativeFunction } = { export const builtins: { [name: string]: Value } = {
// Arithmetic // Arithmetic
'add': { 'add': {
kind: 'native', kind: 'native',
@ -469,14 +469,16 @@ export const builtins: { [name: string]: NativeFunction } = {
arity: 2, arity: 2,
fn: (label, value) => { fn: (label, value) => {
const l = expectString(label, 'debug'); const l = expectString(label, 'debug');
console.log(`[DEBUG] ${l}: `, value);
return value; return value;
} }
}, },
'store': { 'store': {
kind: 'record',
fields: {
'ref': {
kind: 'native', kind: 'native',
name: 'store', name: 'Store.ref',
arity: 1, arity: 1,
fn: (initialValue) => { fn: (initialValue) => {
return { kind: 'ref', value: initialValue }; return { kind: 'ref', value: initialValue };
@ -485,7 +487,7 @@ export const builtins: { [name: string]: NativeFunction } = {
'get': { 'get': {
kind: 'native', kind: 'native',
name: 'get', name: 'Store.get',
arity: 1, arity: 1,
fn: (ref) => { fn: (ref) => {
if (ref.kind !== 'ref') if (ref.kind !== 'ref')
@ -495,9 +497,9 @@ export const builtins: { [name: string]: NativeFunction } = {
} }
}, },
'update': { 'set': {
kind: 'native', kind: 'native',
name: 'update', name: 'Store.set',
arity: 2, arity: 2,
fn: (ref, transformFn) => { fn: (ref, transformFn) => {
return { return {
@ -506,5 +508,7 @@ export const builtins: { [name: string]: NativeFunction } = {
args: [ref, transformFn] args: [ref, transformFn]
}; };
} }
}, }
}
}
} }

@ -11,7 +11,6 @@ import designTokensCode from './design-tokens.cg?raw';
import uiComponentsCode from './ui-components.cg?raw'; import uiComponentsCode from './ui-components.cg?raw';
import textInputCode from './textinput-test.cg?raw'; import textInputCode from './textinput-test.cg?raw';
import refTest from './test-ref.cg?raw';
// import testCode from './test.cg?raw'; // import testCode from './test.cg?raw';
// import counterApp from './counter.cg?raw'; // import counterApp from './counter.cg?raw';
@ -22,7 +21,6 @@ const cgCode = stdlibCode + '\n' +
designTokensCode + '\n' + designTokensCode + '\n' +
uiComponentsCode + '\n' + uiComponentsCode + '\n' +
textInputCode + '\n' textInputCode + '\n'
// refTest + '\n';
try { try {
const tokens = tokenize(cgCode); const tokens = tokenize(cgCode);

@ -279,10 +279,14 @@ export class Parser {
const token = this.current(); const token = this.current();
const params: string[] = []; const params: string[] = [];
while (this.current().kind === 'ident') { while (this.current().kind === 'ident' || this.current().kind === 'underscore') {
const param = this.advance(); const param = this.advance();
if (param.kind === 'underscore') {
params.push('_');
} else {
params.push((param as { value: string }).value); params.push((param as { value: string }).value);
} }
}
this.expect('backslash'); this.expect('backslash');
const body = this.parseExpression(); const body = this.parseExpression();

@ -1,17 +0,0 @@
counter = store 0;
view = state viewport \
currentCount = get counter;
Column {
gap = 10,
children = [
Text { content = str currentCount, x = 30, y = 30 },
Clickable {
event = update counter (n \ n + 1),
child = Rect { w = 100, h = 40, color = "blue" }
}
]
};
{ init = {}, update = state event \ state, view = view }

@ -1,11 +1,14 @@
init = {}; init = {};
email = store.ref "";
password = store.ref "";
update = state event \ event update = state event \ event
# | FocusEmail coords \ state.{ focusedInput = "email" }
# | FocusPassword coords \ state.{ focusedInput = "password" }
| _ \ state; | _ \ state;
view = state viewport \ view = state viewport \
emailText = store.get email;
Positioned { Positioned {
x = 30, x = 30,
y = 30, y = 30,
@ -18,8 +21,7 @@ view = state viewport \
initialFocus = True, initialFocus = True,
w = 300, w = 300,
h = 40, h = 40,
onChange = text \ Noop onChange = text \ store.set email (a \ text)
# onFocus = Noop
}, },
textInput { textInput {
key = "password", key = "password",
@ -27,9 +29,10 @@ view = state viewport \
initialFocus = False, initialFocus = False,
w = 300, w = 300,
h = 40, h = 40,
onChange = text \ Noop onChange = text \ store.set password (a \ text)
# onFocus = Noop },
} Text { content = "Username: " & emailText, x = 8, y = 16 },
Text { content = "Password: " & store.get password, x = 8, y = 16 }
] ]
} }
}; };

@ -88,7 +88,6 @@ textInput = config \ Stateful {
) )
| Char c \ | Char c \
_ = debug c;
newText = insertChar state.text state.cursorPos c; newText = insertChar state.text state.cursorPos c;
newCursorPos = state.cursorPos + 1; newCursorPos = state.cursorPos + 1;
newScroll = calcScrollOffset newText newCursorPos state.scrollOffset 284; newScroll = calcScrollOffset newText newCursorPos state.scrollOffset 284;

Loading…
Cancel
Save