Skip to content

Commit

Permalink
Class OP WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Vihan <contact@vihan.org>
  • Loading branch information
vihanb committed Oct 25, 2016
1 parent 4a3a75c commit 55cd36f
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 12 deletions.
67 changes: 67 additions & 0 deletions package.beta.json
@@ -0,0 +1,67 @@
{
"name": "cheddar-lang-beta",
"description": "Cheddar beta release channel",
"homepage": "http://cheddar.vihan.org/",
"repository": {
"type": "git",
"url": "git+https://github.com/cheddar-lang/Cheddar.git"
},
"author": "Vihan <contact@vihan.org> (http://vihan.org/)",
"tags": [
"javascript",
"language",
"interpreter",
"cheddar",
"cheese",
"food"
],
"version": "1.0.0",
"dependencies": {
"atob": "^2.0.3",
"bases": "^0.2.1",
"btoa": "^1.1.2",
"colors": "^1.1.2",
"commander": "^2.9.0",
"crypto": "0.0.3",
"readline": "^1.3.0",
"xregexp": "^3.1.1"
},
"bin": {
"cheddar-beta": "dist/cli/cheddar"
},
"scripts": {
"prepublish": "make",
"build": "make build",
"test": "make test",
"start": "./dist/cli/cheddar",
"coveralls": "make test && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
"browser_build": "make browser_build"
},
"devDependencies": {
"babel-cli": "^6.11.4",
"babel-core": "^6.13.2",
"babel-istanbul": "^0.11.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-transform-class-properties": "^6.11.5",
"babel-preset-es2015": "^6.13.2",
"browserify": "^13.1.0",
"chai": "^3.5.0",
"codecov.io": "^0.1.6",
"coveralls": "^2.11.12",
"install": "^0.8.1",
"mocha": "^3.0.1",
"npm": "^3.10.6",
"remap-istanbul": "^0.6.4"
},
"main": "dist/cli/cheddar",
"directories": {
"test": "test"
},
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/cheddar-lang/Cheddar/issues"
},
"config": {
"unsafe-perm": true
}
}
1 change: 1 addition & 0 deletions src/interpreter/core/eval/eval.es6
Expand Up @@ -160,6 +160,7 @@ export default class CheddarEval extends CheddarCallStack {

if (OPERATOR === CheddarError.NO_OP_BEHAVIOR) {
NAME = TOKEN.RHS_Operator;
console.log("FAIL...");
run_op(TOKEN, DATA); // Run the operator again
}
}
Expand Down
96 changes: 94 additions & 2 deletions src/interpreter/states/class.es6
Expand Up @@ -3,6 +3,8 @@ import CheddarVariable from '../core/env/var';
import CheddarScope from '../core/env/scope';
import CheddarExec from '../exec';

import CheddarError from '../core/consts/err';

import CheddarFunction from '../core/env/func';

// Gets the appropriate string token from a token list
Expand Down Expand Up @@ -89,6 +91,9 @@ export default class CheddarClassHandler {

newClass.prototype.Scope = new Map();

newClass.OpBinary = new Map();
newClass.OpUnary = new Map();




Expand Down Expand Up @@ -124,8 +129,7 @@ export default class CheddarClassHandler {
// The initalizer
let defaultInitalizer;

// Create the scope
let classScope = new Map();



/** CLASS BODY HANDLING **/
Expand Down Expand Up @@ -185,6 +189,94 @@ export default class CheddarClassHandler {
)
);

} else if (statementName === "ClassStateOp") {




/** OPERATOR OVERLOADING **/
let statementTokens = body[i]._Tokens;

let binaryOrUnary = statementTokens[0];
let operatorLiteral = statementTokens[1];
let callbackArgs = statementTokens[2];
let callbackFunc = statementTokens[3];

if (newClass.Operator.get(operatorLiteral) === CheddarClass.Operator.get(operatorLiteral)) {
newClass.Operator.set(
operatorLiteral,
function(LHS, RHS) {
if (LHS === null) {
if (newClass.OpUnary.has(operatorLiteral)) {
// Execute with RHS
let [
args, data
] = newClass.OpUnary.get(operatorLiteral);

args = args._Tokens.map(i => i._Tokens[0]);

let scope = new CheddarScope(LHS);
scope.setter(
"self",
new CheddarVariable(
RHS,
{
Writeable: false
}
)
);

if (args[0]) scope.setter(args[0], new CheddarVariable(RHS, { Writeable: false }));
return new CheddarExec(tostr(data), scope, {
perms: newClass
}).exec();
}
} else if (newClass.OpBinary.has(operatorLiteral)) {
// Execute with LHS, RHS
let [
args, data
] = newClass.OpBinary.get(operatorLiteral);

args = args._Tokens.map(i => i._Tokens[0]);

let scope = new CheddarScope(LHS);
scope.setter(
"self",
new CheddarVariable(
LHS,
{
Writeable: false
}
)
);

// TODO: enforce constants
if (args[0]) scope.setter(args[0], new CheddarVariable(LHS, { Writeable: false }));
if (args[1]) scope.setter(args[1], new CheddarVariable(RHS, { Writeable: false }));

return new CheddarExec(tostr(data), scope, {
perms: newClass
}).exec();
} else {
return CheddarError.NO_OP_BEHAVIOR;
}
}
)
}

// Assign codeblocks
if (binaryOrUnary === 'binary') {
newClass.OpBinary.set(
operatorLiteral,
[callbackArgs, callbackFunc]
)
} else if (binaryOrUnary === 'unary') {
newClass.OpUnary.set(
operatorLiteral,
[callbackArgs, callbackFunc]
)
}

}

}
Expand Down
3 changes: 2 additions & 1 deletion src/tokenizer/consts/err.es6
Expand Up @@ -10,4 +10,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 ALLOW_ERROR = Symbol('er_ALLOW_ERROR');
export const ALLOW_ERROR = Symbol('er_ALLOW_ERROR');
export const KEEP_ITEM = Symbol('er_KEEP_ITEM');
9 changes: 7 additions & 2 deletions src/tokenizer/states/class.es6
Expand Up @@ -17,8 +17,13 @@ export default class StatementClass extends CheddarLexer {
this.open(false);

return this.grammar(true,
['class', this.jumpWhite, CheddarVariableToken, [ClassArguments], '{',
CheddarCustomLexer(ClassStatement, tokenizer), '}'
[
'class', this.jumpWhite, CheddarVariableToken,
[ClassArguments],
[['extends', this.jumpWhite, CheddarVariableToken]],
'{',
CheddarCustomLexer(ClassStatement, tokenizer),
'}'
]
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/tokenizer/states/class/single_state.es6
Expand Up @@ -3,6 +3,7 @@ import * as CheddarError from '../../consts/err';

import ClassStateInit from './states/init';
import ClassStateFunc from './states/method';
import ClassStateOp from './states/op';

export default class ClassSingleStatement extends CheddarLexer {
exec(tokenizer) {
Expand All @@ -12,7 +13,8 @@ export default class ClassSingleStatement extends CheddarLexer {
/* Class Statement List */
[
ClassStateInit,
ClassStateFunc
ClassStateFunc,
ClassStateOp
], tokenizer
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/tokenizer/states/class/states.es6
Expand Up @@ -16,7 +16,7 @@ export default class ClassStatement extends CheddarLexer {
items.push(res);
this.Index = res.Index;

//this.jumpLiteral(';');
this.jumpLiteral(';');

match = new ClassSingleStatement(this.Code, this.Index);
res = match.exec(tokenizer);
Expand Down
29 changes: 29 additions & 0 deletions src/tokenizer/states/class/states/op.es6
@@ -0,0 +1,29 @@
import CheddarLexer from '../../../tok/lex';
import CheddarCodeblock from '../../../patterns/block';
import CheddarCustomLexer from '../../../parsers/custom';

import CheddarArray from '../../../parsers/array';
import CheddarVariable from '../../../literals/var';

import {KEEP_ITEM} from '../../../consts/err';

import {UOP, OP} from '../../../consts/ops';

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

return this.grammar(true,
[
'unary', KEEP_ITEM, 'op', UOP,
CheddarCustomLexer(CheddarArray, '(', ')', CheddarVariable),
CheddarCustomLexer(CheddarCodeblock, tokenizer)
],
[
'binary', KEEP_ITEM, 'op', OP,
CheddarCustomLexer(CheddarArray, '(', ')', CheddarVariable),
CheddarCustomLexer(CheddarCodeblock, tokenizer)
]
);
}
}
12 changes: 11 additions & 1 deletion src/tokenizer/tok/lex.es6
Expand Up @@ -237,6 +237,12 @@ export default class CheddarLexer {
index += defs[i][j][0].length;
tokens.push(defs[i][j][0]);
}
} else if (defs[i][j][0] === this.jumpWhite) {
let oldIndex = this.Index;
this.Index = index;
this.jumpWhite();
index = this.Index;
this.Index = oldIndex;
} else {
parser = this.initParser(defs[i][j][0], index);
result = parser.exec();
Expand Down Expand Up @@ -313,8 +319,12 @@ export default class CheddarLexer {
if (result)
index = this.Index;
this.Index = oldIndex;
if (!result)
if (!result) {
continue main;
} else if (defs[i][j + 1] === CheddarError.KEEP_ITEM) {
tokens.push(defs[i][j]);
j++;
}
}
}

Expand Down
18 changes: 14 additions & 4 deletions test/cheddar/class.cheddar
@@ -1,11 +1,21 @@
class Animal (private string: name, public number: age) {
class Animal (public string: name, public number: age) {
init {
print "Created an animal named %s" % self.name
print "Created an animal named %s" % self.name;
}

func getName -> self.name
binary op + (a, b) {
print a.name;
print b.name;
}

func getName -> self.name;
func getAge -> self.age
}

let myDog: Animal = Animal { "Max", 3 };
print "It is %d years old" % myDog.age;
print "It's name is %s" % myDog.getName();
print "It's name is %s" % myDog.getName();
print "It's age is %d" % myDog.getAge();

let myGoat: Animal = Animal { "Downgoat", 5 };
print myDog + myGoat;

0 comments on commit 55cd36f

Please sign in to comment.