Skip to content

Commit

Permalink
Merge pull request #60 from cheddar-lang/feature-funcop
Browse files Browse the repository at this point in the history
Functionized Operators
  • Loading branch information
vihanb committed Jul 14, 2016
2 parents c3dad6a + 1284a5a commit ecb2060
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 5 deletions.
11 changes: 11 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FEATURE:
- [x] Functionized Operators
- [ ] Custom REPL Colors
- [ ] Strict typing in assignment
- [ ] Static enforcement in functions
- [ ] Class literals
- [ ] Func statement
- [ ] Signal propogation
- [x] for of

FIX:
6 changes: 6 additions & 0 deletions src/interpreter/core/config/link.es6
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ export const PRIMITIVE_LINKS = new Map([
["CheddarNumberToken" , CheddarNumber],
["CheddarArrayToken" , CheddarArray],
["CheddarFunctionToken", CheddarFunc]
]);

import CheddarFunctionizedOperator from '../evaluated/fop';

export const EVALUATED_LINKS = new Map([
["CheddarFunctionizedOperatorToken", CheddarFunctionizedOperator]
]);
9 changes: 5 additions & 4 deletions src/interpreter/core/eval/eval.es6
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// itself

// Primitive <-> Class Links
import {PRIMITIVE_LINKS} from '../config/link';
import {PRIMITIVE_LINKS, EVALUATED_LINKS} from '../config/link';
import {TYPE as OP_TYPE, EXCLUDE_META_ASSIGNMENT as REG_OPS} from '../../../tokenizer/consts/ops';

// Reference tokens
Expand Down Expand Up @@ -154,7 +154,7 @@ export default class CheddarEval extends CheddarCallStack {
OPERATOR = CheddarError.NO_OP_BEHAVIOR;
}
} else {
// Binary operator. DATA is RHS, TOKEN is LHS
// Binary operator. DATA is LHS, TOKEN is RHS
DATA = this.shift(); // Get the other arg

NAME = DATA.constructor.Operator ||
Expand Down Expand Up @@ -242,8 +242,7 @@ export default class CheddarEval extends CheddarCallStack {
}

// Get the class associated with the token
OPERATOR = PRIMITIVE_LINKS.get(TOKEN.constructor.name);
if (OPERATOR) {
if ((OPERATOR = PRIMITIVE_LINKS.get(TOKEN.constructor.name))) {
// Set the name to be used in errors
NAME = OPERATOR.Name || "object";

Expand All @@ -258,6 +257,8 @@ export default class CheddarEval extends CheddarCallStack {
this.put(OPERATOR);
return true;
}
} else if ((OPERATOR = EVALUATED_LINKS.get(TOKEN.constructor.name))) {
OPERATOR = OPERATOR(...TOKEN.Tokens);
} else {
return CheddarError.UNLINKED_CLASS;
}
Expand Down
49 changes: 49 additions & 0 deletions src/interpreter/core/evaluated/fop.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import CheddarFunction from '../env/func';
import CheddarError from '../consts/err';
import CheddarClass from '../env/class';
import CheddarErrorDesc from '../consts/err_msg';

export default function(operator) {
return new CheddarFunction([
["a", {}],
["b", {}]
], function(scope, input) {
let LHS = input("a");
let RHS = input("b");

let resp; // output / response
let opfunc = (
LHS.constructor.Operator ||
LHS.Operator
).get(operator);

if (opfunc) {
resp = opfunc(LHS, RHS);
}
else {
resp = CheddarError.NO_OP_BEHAVIOR;
}

if (resp === CheddarError.NO_OP_BEHAVIOR) {
return CheddarErrorDesc.get(resp)
.replace(/\$0/g, operator)
.replace(/\$1/g, LHS ? (
LHS.constructor.Name || (
LHS.prototype instanceof CheddarClass ?
"Class" :
"nil"
)
) : "nil")
.replace(/\$2/g, RHS ? (
RHS.constructor.Name || (
RHS.prototype instanceof CheddarClass ?
"Class" :
"nil"
)
) : "nil");
} else {
return resp;
}

});
}
11 changes: 11 additions & 0 deletions src/tokenizer/literals/fop.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Functionized operators
import {OP} from '../consts/ops';
import CheddarPrimitive from './primitive';

export default class CheddarFunctionizedOperatorToken extends CheddarPrimitive {
exec() {
return this.grammar(true, [
'(', OP, ')'
]);
}
}
1 change: 0 additions & 1 deletion src/tokenizer/literals/op.es6
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import CheddarLexer from '../tok/lex';
import {UOP, OP} from '../consts/ops';

export default class CheddarOperatorToken extends CheddarLexer {
// Chatse
exec(UNARY) {
let ops = UNARY ? UOP : OP;
// this.Code is the code
Expand Down
3 changes: 3 additions & 0 deletions src/tokenizer/parsers/any.es6
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import CheddarStringToken from '../literals/string';
import CheddarNumberToken from '../literals/number';
import CheddarArrayToken from './array';

import CheddarFunctionizedOperatorToken from '../literals/fop';

export default class CheddarAnyLiteral extends CheddarLexer {
exec() {
this.open(false);

let attempt = this.attempt(
CheddarFunctionizedOperatorToken,
CheddarStringToken,
CheddarNumberToken,
CheddarBooleanToken,
Expand Down
Empty file added src/tokenizer/states/func.es6
Empty file.

0 comments on commit ecb2060

Please sign in to comment.