diff --git a/src/builtins.ts b/src/builtins.ts index ed88f64..b1193fd 100644 --- a/src/builtins.ts +++ b/src/builtins.ts @@ -36,7 +36,7 @@ function expectString(v: Value, name: string): string { // return v.elements; // } -export const builtins: { [name: string]: NativeFunction } = { +export const builtins: { [name: string]: Value } = { // Arithmetic 'add': { kind: 'native', @@ -469,42 +469,46 @@ export const builtins: { [name: string]: NativeFunction } = { arity: 2, fn: (label, value) => { const l = expectString(label, 'debug'); - console.log(`[DEBUG] ${l}: `, value); return value; } }, 'store': { - kind: 'native', - name: 'store', - arity: 1, - fn: (initialValue) => { - return { kind: 'ref', value: initialValue }; - } - }, - - 'get': { - kind: 'native', - name: 'get', - arity: 1, - fn: (ref) => { - if (ref.kind !== 'ref') - throw new Error('get expects a Ref'); + kind: 'record', + fields: { + 'ref': { + kind: 'native', + name: 'Store.ref', + arity: 1, + fn: (initialValue) => { + return { kind: 'ref', value: initialValue }; + } + }, - return ref.value; - } - }, + 'get': { + kind: 'native', + name: 'Store.get', + arity: 1, + fn: (ref) => { + if (ref.kind !== 'ref') + throw new Error('get expects a Ref'); - 'update': { - kind: 'native', - name: 'update', - arity: 2, - fn: (ref, transformFn) => { - return { - kind: 'constructor', - name: 'Update', - args: [ref, transformFn] - }; + return ref.value; + } + }, + + 'set': { + kind: 'native', + name: 'Store.set', + arity: 2, + fn: (ref, transformFn) => { + return { + kind: 'constructor', + name: 'Update', + args: [ref, transformFn] + }; + } + } } - }, + } } diff --git a/src/main.ts b/src/main.ts index a098919..cfcb386 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,7 +11,6 @@ import designTokensCode from './design-tokens.cg?raw'; import uiComponentsCode from './ui-components.cg?raw'; import textInputCode from './textinput-test.cg?raw'; -import refTest from './test-ref.cg?raw'; // import testCode from './test.cg?raw'; // import counterApp from './counter.cg?raw'; @@ -22,7 +21,6 @@ const cgCode = stdlibCode + '\n' + designTokensCode + '\n' + uiComponentsCode + '\n' + textInputCode + '\n' - // refTest + '\n'; try { const tokens = tokenize(cgCode); diff --git a/src/parser.ts b/src/parser.ts index ed18b02..8c83bdf 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -279,9 +279,13 @@ export class Parser { const token = this.current(); const params: string[] = []; - while (this.current().kind === 'ident') { + while (this.current().kind === 'ident' || this.current().kind === 'underscore') { const param = this.advance(); - params.push((param as { value: string }).value); + if (param.kind === 'underscore') { + params.push('_'); + } else { + params.push((param as { value: string }).value); + } } this.expect('backslash'); diff --git a/src/test-ref.cg b/src/test-ref.cg deleted file mode 100644 index 34bb75c..0000000 --- a/src/test-ref.cg +++ /dev/null @@ -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 } diff --git a/src/textinput-test.cg b/src/textinput-test.cg index eddf274..7e24c2e 100644 --- a/src/textinput-test.cg +++ b/src/textinput-test.cg @@ -1,11 +1,14 @@ init = {}; +email = store.ref ""; +password = store.ref ""; + update = state event \ event - # | FocusEmail coords \ state.{ focusedInput = "email" } - # | FocusPassword coords \ state.{ focusedInput = "password" } | _ \ state; view = state viewport \ + emailText = store.get email; + Positioned { x = 30, y = 30, @@ -18,8 +21,7 @@ view = state viewport \ initialFocus = True, w = 300, h = 40, - onChange = text \ Noop - # onFocus = Noop + onChange = text \ store.set email (a \ text) }, textInput { key = "password", @@ -27,9 +29,10 @@ view = state viewport \ initialFocus = False, w = 300, h = 40, - onChange = text \ Noop - # onFocus = Noop - } + onChange = text \ store.set password (a \ text) + }, + Text { content = "Username: " & emailText, x = 8, y = 16 }, + Text { content = "Password: " & store.get password, x = 8, y = 16 } ] } }; diff --git a/src/ui-components.cg b/src/ui-components.cg index b6f3566..052b5f3 100644 --- a/src/ui-components.cg +++ b/src/ui-components.cg @@ -88,7 +88,6 @@ textInput = config \ Stateful { ) | Char c \ - _ = debug c; newText = insertChar state.text state.cursorPos c; newCursorPos = state.cursorPos + 1; newScroll = calcScrollOffset newText newCursorPos state.scrollOffset 284;