Skip to content

Commit

Permalink
Merge 36f065e into 284058b
Browse files Browse the repository at this point in the history
  • Loading branch information
vihanb committed Nov 27, 2016
2 parents 284058b + 36f065e commit e5e6291
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 95 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -20,7 +20,7 @@
"atob": "^2.0.3",
"bases": "^0.2.1",
"btoa": "^1.1.2",
"cheddar-parser": "~0.2.2",
"cheddar-parser": "~0.3.0",
"colors": "^1.1.2",
"commander": "^2.9.0",
"crypto": "0.0.3",
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/core/consts/dict.es6
@@ -1 +1 @@
export const KEY_INTERNAL = new Set("String", "Number");
export const KEY_INTERNAL = new Set(["String", "Number"]);
4 changes: 1 addition & 3 deletions src/interpreter/core/env/defaults.es6
Expand Up @@ -11,12 +11,10 @@ export const DEFAULT_OP = new Map([
if (!LHS || !LHS.Cast)
return CheddarError.NO_OP_BEHAVIOR;

// Attempt to call `repr`, else, cast to string
// get the string representation
let VAL = LHS.constructor.Name === 'String' ? LHS
: LHS.Cast.has('String')
? LHS.Cast.get('String')(LHS)
: LHS.Operator.has('repr')
? LHS.Operator.get('repr')(null, LHS)
: LHS;


Expand Down
42 changes: 35 additions & 7 deletions src/interpreter/core/primitives/Dictionary.es6
Expand Up @@ -6,10 +6,19 @@ import { KEY_INTERNAL } from '../consts/dict';
import BehaviorOperator from './op/dict';
import BehaviorCast from './cast/dict';

import NIL from '../consts/nil';
import CheddarVariable from '../env/var';

import CheddarScope from '../env/scope';

function evaluate(item, scope) {
return new CheddarEval({ _Tokens: [item] }, scope).exec();
}

function toRepr(item) {
return KEY_INTERNAL.has(item.constructor.Name) ? item.value : item;
}

export default class CheddarDictionary extends CheddarClass {
static Name = "Dictionary";

Expand All @@ -31,20 +40,39 @@ export default class CheddarDictionary extends CheddarClass {

let tok_value = evaluate(entry[1], this.scope);

if (typeof tok_key === "string") {
return tok_key;
if (typeof tok_value === "string") {
return tok_value;
}

if (KEY_INTERNAL.has(tok_key.constructor.Name)) {
this.value.set(tok_key.value, tok_value);
} else {
this.value.set(tok_key, tok_value);
}
this.value.set(toRepr(tok_key), tok_value);
}

// evaluated accessor boilerplate
this.scope_ref = new CheddarScope();
let scope_ref_setter = this.scope_ref.setter;
this.scope_ref.setter = (path, res) => {
this.value.set(path, res.Value);
scope_ref_setter.call(this.scope_ref, path, res);
};

return true;
}

eval_accessor(token) {
// Go ahead and grab token
let val = this.value.get(toRepr(token));

if (!val) return new CheddarVariable(new NIL);

// Scope boilerplate
val.scope = this.scope_ref;
val.Reference = toRepr(token);

this.scope_ref.setter(val.Reference, val = new CheddarVariable(val));

return val;
}

Operator = new Map([...CheddarClass.Operator, ...BehaviorOperator]);
Cast = BehaviorCast;
}
Expand Down
1 change: 0 additions & 1 deletion src/interpreter/core/primitives/String.es6
Expand Up @@ -30,7 +30,6 @@ export default class CheddarString extends CheddarClass {
scope_ref_setter.call(this.scope_ref, path, res);
};


return true;
}

Expand Down
7 changes: 2 additions & 5 deletions src/interpreter/core/primitives/cast/array.es6
Expand Up @@ -9,14 +9,11 @@ export default new Map([
Cast;
for (let i = 0; i < self.value.length; i++) {
Cast = self.value[i] && self.value[i].Cast ?
self.value[i].Cast.has('String') ||
self.value[i].Operator.has('repr') : false;
self.value[i].Cast.has('String') : false;

if (Cast)
Stringified += (i ? ", " : "") + (
self.value[i].Cast.has('String') ?
self.value[i].Cast.get('String')(self.value[i]) :
self.value[i].Operator.get('repr')(null, self.value[i])
self.value[i].Cast.get('String')(self.value[i])
).value;
else
Stringified += (i ? ", " : "") + `<${self.value[i].constructor.Name || self.value[i].Name}>`;
Expand Down
42 changes: 42 additions & 0 deletions src/interpreter/core/primitives/cast/dict.es6
@@ -1,5 +1,47 @@
import CheddarString from '../String';
import HelperInit from '../../../../helpers/init';

function padItem(str) {
let lines = str.split(/\r?\n/g);
if (lines.length < 2) return str;

for (var i = 1; i < lines.length; i++) {
lines[i] = ' ' + lines[i];
}

return lines.join("\n");
}

export default new Map([
['String', (self) => {
// Output strings without delimiter
var string = "";

// From each key, obtain the string representation of both sides
self.value.forEach((value, key) => {
let [k, v] = [key, value].map(
// Convert `item` to string representation
// pad with 4 spaces then
(item) => {
// Get the primitive value
let value;
if (typeof item !== 'object' && typeof item !== 'function') {
// Handle primitives
value = typeof item == 'string' ? JSON.stringify(item) : item;
} else {
value = item.Cast && item.Cast.has("String") ? item.Cast.get("String")(item).value : `<${item.Name || item.constructor.Name}>`;
}

if (item instanceof self.constructor) return padItem(value);
else return value;
}
);

string += `\n ${k}: ${v}`;
});

if (!string) return HelperInit(CheddarString, "[:]");

return HelperInit(CheddarString, `[${string}\n]`);
}]
]);
3 changes: 1 addition & 2 deletions src/interpreter/core/primitives/cast/regex.es6
Expand Up @@ -6,7 +6,6 @@ import HelperInit from '../../../../helpers/init';
export default new Map([
['String', (self) => {
let CheddarString = require('../String');

return HelperInit(CheddarString, self.source);
return HelperInit(CheddarString, `/${self.value.xregexp.source}/${self.value.xregexp.flags}`);
}]
]);
7 changes: 6 additions & 1 deletion src/interpreter/core/primitives/cast/string.es6
Expand Up @@ -18,7 +18,12 @@ export default new Map([
return HelperInit(CheddarNumber, ...Attempt._Tokens);
else
return CheddarError.CAST_FAILED;
}]
}],
['String', (self) => {
return HelperInit(self.constructor, '"' + self.value.replace(
/"|\\/g, "\\$&"
) + '"');
}],
]);

/*
Expand Down
5 changes: 0 additions & 5 deletions src/interpreter/core/primitives/op/regex.es6
Expand Up @@ -2,11 +2,6 @@ import HelperInit from '../../../../helpers/init';
import XRegExp from 'xregexp';

export default new Map([
['repr', (LHS, RHS) => {
let CheddarString = require('../String');
return HelperInit(CheddarString, `/${RHS.value.xregexp.source}/${RHS.value.xregexp.flags}`);
}],

['|', (LHS, RHS) => {
if (LHS && RHS instanceof LHS.constructor) {
return HelperInit(LHS.constructor, XRegExp.union([LHS.value, RHS.value]));
Expand Down
9 changes: 0 additions & 9 deletions src/interpreter/core/primitives/op/string.es6
Expand Up @@ -6,15 +6,6 @@ import HelperInit from '../../../../helpers/init';
import sprintf from '../../../../stdlib/ns/IO/sprintf';
// == STRING ==
export default new Map([
// Replace " with \"
// and replace \
// with \\s
['repr', (_, LHS) => {
return HelperInit(LHS.constructor, '"' + LHS.value.replace(
/"|\\/g, "\\$&"
) + '"');
}],

// String concatenation
// using +, attempt to
// implicitly cast
Expand Down
2 changes: 2 additions & 0 deletions src/stdlib/api.es6
@@ -1,3 +1,4 @@
import CheddarDictionary from '../interpreter/core/primitives/Dictionary';
import CheddarString from '../interpreter/core/primitives/String';
import CheddarSymbol from '../interpreter/core/primitives/Symbol';
import CheddarNumber from '../interpreter/core/primitives/Number';
Expand All @@ -16,6 +17,7 @@ import CheddarClass from '../interpreter/core/env/class';
import HelperInit from '../helpers/init';

var API = {
dictionary: CheddarDictionary,
string: CheddarString,
symbol: CheddarSymbol,
number: CheddarNumber,
Expand Down
1 change: 1 addition & 0 deletions src/stdlib/stdlib.es6
Expand Up @@ -30,6 +30,7 @@ STDLIB.p("Symbol", API.symbol);
STDLIB.p("Regex", API.regex);
STDLIB.p("Number", API.number);
STDLIB.p("Array", API.array);
STDLIB.p("Dictionary", API.dictionary);
STDLIB.p("Boolean", API.bool);
STDLIB.p("Function", API.func);

Expand Down
51 changes: 0 additions & 51 deletions test/tests/Primitives/Op/Regex.js

This file was deleted.

16 changes: 8 additions & 8 deletions test/tests/primitives/op/Regex.js
Expand Up @@ -4,47 +4,47 @@ var expect = chai.expect;
chai.should();

describe('Regex', function() {
describe('repr', function() {
describe('String::', function() {
it('should work', TestCheddarFrom.Code(
'print repr /.+/',
'print String::/.+/',
'/.+/'
))

it('should work with transpiled items', TestCheddarFrom.Code(
'print repr /(?<a>b)/',
'print String::/(?<a>b)/',
'/(?<a>b)/'
))

it('should work with flags', TestCheddarFrom.Code(
'print repr /(?<a>b)/gi',
'print String::/(?<a>b)/gi',
'/(?<a>b)/gi'
))
})

describe('|', function() {
it('should work', TestCheddarFrom.Code(
'print repr ( /a/ | /b/ )',
'print String::( /a/ | /b/ )',
'/a|b/'
))
})

describe('+', function() {
it('should work', TestCheddarFrom.Code(
'print repr ( /a/ + /b/ )',
'print String::( /a/ + /b/ )',
'/ab/'
))
})

describe('-', function() {
it('should work', TestCheddarFrom.Code(
'print repr ( /a/ - /b/ )',
'print String::( /a/ - /b/ )',
'/(?!b)(?:a)/'
))
})

describe('*', function() {
it('should work', TestCheddarFrom.Code(
'print repr ( /a/ * 3 )',
'print String::( /a/ * 3 )',
'/(?:a){3}/'
))
})
Expand Down
2 changes: 1 addition & 1 deletion vbump.sh
Expand Up @@ -56,7 +56,7 @@ echo;
echo "Sucesfully prepared release"
echo;

git tag -a "$VERSION" -m "$VERSION"
git tag -a "$VERSION"
echo;

echo "Created tag for ${bold}${VERSION}${normal}"
Expand Down

0 comments on commit e5e6291

Please sign in to comment.