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;
|
// return v.elements;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
export const builtins: { [name: string]: NativeFunction } = {
|
export const builtins: { [name: string]: Value } = {
|
||||||
// Arithmetic
|
// Arithmetic
|
||||||
'add': {
|
'add': {
|
||||||
kind: 'native',
|
kind: 'native',
|
||||||
|
|
@ -469,42 +469,46 @@ 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: 'native',
|
kind: 'record',
|
||||||
name: 'store',
|
fields: {
|
||||||
arity: 1,
|
'ref': {
|
||||||
fn: (initialValue) => {
|
kind: 'native',
|
||||||
return { kind: 'ref', value: initialValue };
|
name: 'Store.ref',
|
||||||
}
|
arity: 1,
|
||||||
},
|
fn: (initialValue) => {
|
||||||
|
return { kind: 'ref', value: initialValue };
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
'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')
|
||||||
throw new Error('get expects a Ref');
|
throw new Error('get expects a Ref');
|
||||||
|
|
||||||
return ref.value;
|
return ref.value;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
'update': {
|
'set': {
|
||||||
kind: 'native',
|
kind: 'native',
|
||||||
name: 'update',
|
name: 'Store.set',
|
||||||
arity: 2,
|
arity: 2,
|
||||||
fn: (ref, transformFn) => {
|
fn: (ref, transformFn) => {
|
||||||
return {
|
return {
|
||||||
kind: 'constructor',
|
kind: 'constructor',
|
||||||
name: 'Update',
|
name: 'Update',
|
||||||
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,9 +279,13 @@ 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();
|
||||||
params.push((param as { value: string }).value);
|
if (param.kind === 'underscore') {
|
||||||
|
params.push('_');
|
||||||
|
} else {
|
||||||
|
params.push((param as { value: string }).value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.expect('backslash');
|
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 = {};
|
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…
Add table
Add a link
Reference in a new issue