Skip to content

Commit

Permalink
feature [assign]: Added implicit strict typing with :=
Browse files Browse the repository at this point in the history
Using `:=` instead of `=` in an assignment. The variable will be
implicitly declared strictly
  • Loading branch information
vihanb committed Oct 2, 2016
1 parent 42e53b8 commit 9d4851c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/interpreter/states/assign.es6
Expand Up @@ -33,10 +33,15 @@ export default class CheddarAssign {
let res;

if (this.toks.tok(2)) {
let val = new CheddarEval({ _Tokens: [this.toks.tok(2)] }, this.scope);

let val = new CheddarEval({ _Tokens: [this.toks.tok(3)] }, this.scope);
if (!((val = val.exec()) instanceof CheddarClass || val.prototype instanceof CheddarClass))
return val;

if (this.toks.tok(2) === ':=') {
stricttype = val.constructor;
}

if (stricttype && !(val instanceof stricttype)) {
return `Attempted to set \`${varname}\` to a \`${
val.Name ||
Expand Down
3 changes: 2 additions & 1 deletion src/tokenizer/consts/err.es6
Expand Up @@ -9,4 +9,5 @@
export const EXIT_NOTFOUND = Symbol('er_EXIT_NOTFOUND');
export const UNEXPECTED_TOKEN = Symbol('er_UNEXPECTED_TOKEN');
export const UNMATCHED_DELIMITER = Symbol('er_UNMATCHED_DELIMITER');
export const EXPECTED_BLOCK = Symbol('er_EXPECTED_BLOCK');
export const EXPECTED_BLOCK = Symbol('er_EXPECTED_BLOCK');
export const ALLOW_ERROR = Symbol('er_ALLOW_ERROR');
6 changes: 4 additions & 2 deletions src/tokenizer/states/assign.es6
Expand Up @@ -12,8 +12,10 @@ export default class StatementAssign extends CheddarLexer {
let DEFS = ['var', 'let', 'const'];
return this.grammar(true,
[
DEFS, this.jumpWhite, CheddarTypedVariableToken, CheddarError.UNEXPECTED_TOKEN,
[['=', CheddarExpressionToken]]
DEFS, this.jumpWhite, CheddarTypedVariableToken, [':=', '='], CheddarError.ALLOW_ERROR, CheddarExpressionToken
],
[
DEFS, this.jumpWhite, CheddarTypedVariableToken
]
);
}
Expand Down
16 changes: 13 additions & 3 deletions src/tokenizer/tok/lex.es6
Expand Up @@ -177,8 +177,13 @@ export default class CheddarLexer {
!(result instanceof CheddarLexer) &&
typeof defs[i][j + 1] === 'symbol'
) {
this.Index = Math.max(parser.Index, index);
return this.error(defs[i][j + 1]);
if (defs[i][j + 1] === CheddarError.ALLOW_ERROR) {
j++;
continue main;
} else {
this.Index = Math.max(parser.Index, index);
return this.error(defs[i][j + 1]);
}
}

if (result === CheddarError.EXIT_NOTFOUND) {
Expand Down Expand Up @@ -292,7 +297,12 @@ export default class CheddarLexer {
}
} else {
// this.Index = result.Index;
return this.error(CheddarError.EXIT_NOTFOUND);
if (defs[i][j + 1] === CheddarError.ALLOW_ERROR) {
j++;
continue main;
} else {
return this.error(CheddarError.EXIT_NOTFOUND);
}
}
}
} else {
Expand Down
5 changes: 5 additions & 0 deletions test/tests/expr/assignment.js
Expand Up @@ -28,6 +28,11 @@ describe('Assignment', function() {
'var a: Number = 1; print a',
'1'
))

it('should work with implicit definition', TestCheddarFrom.Code(
`let a := 1; a = 2`,
''
))
})

describe('reassignment', function() {
Expand Down

0 comments on commit 9d4851c

Please sign in to comment.