namespacing the store functions. allowing _ in params
This commit is contained in:
parent
1af838126e
commit
9b8916eb72
6 changed files with 50 additions and 59 deletions
|
|
@ -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 };
|
||||
}
|
||||
},
|
||||
kind: 'record',
|
||||
fields: {
|
||||
'ref': {
|
||||
kind: 'native',
|
||||
name: 'Store.ref',
|
||||
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');
|
||||
'get': {
|
||||
kind: 'native',
|
||||
name: 'Store.get',
|
||||
arity: 1,
|
||||
fn: (ref) => {
|
||||
if (ref.kind !== 'ref')
|
||||
throw new Error('get expects a Ref');
|
||||
|
||||
return ref.value;
|
||||
}
|
||||
},
|
||||
return ref.value;
|
||||
}
|
||||
},
|
||||
|
||||
'update': {
|
||||
kind: 'native',
|
||||
name: 'update',
|
||||
arity: 2,
|
||||
fn: (ref, transformFn) => {
|
||||
return {
|
||||
kind: 'constructor',
|
||||
name: 'Update',
|
||||
args: [ref, transformFn]
|
||||
};
|
||||
'set': {
|
||||
kind: 'native',
|
||||
name: 'Store.set',
|
||||
arity: 2,
|
||||
fn: (ref, transformFn) => {
|
||||
return {
|
||||
kind: 'constructor',
|
||||
name: 'Update',
|
||||
args: [ref, transformFn]
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -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 = {};
|
||||
|
||||
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 }
|
||||
]
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue