Skip to content

Commit

Permalink
Merge pull request #36 from mohayonao/add-accidental-number
Browse files Browse the repository at this point in the history
support accidental number literal e.g. 2ss, 2b50
  • Loading branch information
mohayonao committed May 14, 2014
2 parents 69e21cd + fd90672 commit 4137671
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
35 changes: 34 additions & 1 deletion build/scscript.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function(global) {
"use strict";

var sc = { VERSION: "0.0.26" };
var sc = { VERSION: "0.0.27" };

// src/sc/sc.js
(function(sc) {
Expand Down Expand Up @@ -1659,6 +1659,7 @@ var sc = { VERSION: "0.0.26" };
Lexer.prototype.scanNumericLiteral = function(neg) {
return this.scanNAryNumberLiteral(neg) ||
this.scanHexNumberLiteral(neg) ||
this.scanAccidentalNumberLiteral(neg) ||
this.scanDecimalNumberLiteral(neg);
};

Expand Down Expand Up @@ -1810,6 +1811,38 @@ var sc = { VERSION: "0.0.26" };
return this.makeToken(token.type, token.value, start);
};

Lexer.prototype.scanAccidentalNumberLiteral = function(neg) {
var re, start, items;
var integer, accidental, cents;
var sign, value;
var token;

re = /^(\d+)([bs]+)(\d*)/;
start = this.index;
items = re.exec(this.source.slice(this.index));

if (!items) {
return;
}

integer = items[1];
accidental = items[2];
sign = (accidental.charAt(0) === "s") ? +1 : -1;

if (items[3] === "") {
cents = Math.min(accidental.length * 0.1, 0.4);
} else {
cents = Math.min(items[3] * 0.001, 0.499);
}
value = +integer + (sign * cents);

token = makeNumberToken(Token.FloatLiteral, value, neg, false);

this.index += items[0].length;

return this.makeToken(token.type, token.value, start);
};

Lexer.prototype.scanDecimalNumberLiteral = function(neg) {
var re, start, items, integer, frac, pi;
var value, type;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scscript",
"version": "0.0.26",
"version": "0.0.27",
"author": "Nao Yonamine <mohayonao@gmail.com>",
"homepage": "http://mohayonao.github.io/SCScript/",
"bugs": "https://github.com/mohayonao/SCScript/issues",
Expand Down
33 changes: 33 additions & 0 deletions src/sc/lang/compiler/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@
Lexer.prototype.scanNumericLiteral = function(neg) {
return this.scanNAryNumberLiteral(neg) ||
this.scanHexNumberLiteral(neg) ||
this.scanAccidentalNumberLiteral(neg) ||
this.scanDecimalNumberLiteral(neg);
};

Expand Down Expand Up @@ -511,6 +512,38 @@
return this.makeToken(token.type, token.value, start);
};

Lexer.prototype.scanAccidentalNumberLiteral = function(neg) {
var re, start, items;
var integer, accidental, cents;
var sign, value;
var token;

re = /^(\d+)([bs]+)(\d*)/;
start = this.index;
items = re.exec(this.source.slice(this.index));

if (!items) {
return;
}

integer = items[1];
accidental = items[2];
sign = (accidental.charAt(0) === "s") ? +1 : -1;

if (items[3] === "") {
cents = Math.min(accidental.length * 0.1, 0.4);
} else {
cents = Math.min(items[3] * 0.001, 0.499);
}
value = +integer + (sign * cents);

token = makeNumberToken(Token.FloatLiteral, value, neg, false);

this.index += items[0].length;

return this.makeToken(token.type, token.value, start);
};

Lexer.prototype.scanDecimalNumberLiteral = function(neg) {
var re, start, items, integer, frac, pi;
var value, type;
Expand Down
44 changes: 44 additions & 0 deletions src/sc/lang/compiler/lexer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,50 @@
}
},
],
"2bb": [
{
type: Token.FloatLiteral,
value: "1.8",
range: [ 0, 3 ],
loc: {
start: { line: 1, column: 0 },
end : { line: 1, column: 3 }
}
}
],
"2ss": [
{
type: Token.FloatLiteral,
value: "2.2",
range: [ 0, 3 ],
loc: {
start: { line: 1, column: 0 },
end : { line: 1, column: 3 }
}
}
],
"2b10": [
{
type: Token.FloatLiteral,
value: "1.99",
range: [ 0, 4 ],
loc: {
start: { line: 1, column: 0 },
end : { line: 1, column: 4 }
}
}
],
"2s10": [
{
type: Token.FloatLiteral,
value: "2.01",
range: [ 0, 4 ],
loc: {
start: { line: 1, column: 0 },
end : { line: 1, column: 4 }
}
}
],
"pi": [
{
type: Token.FloatLiteral,
Expand Down

0 comments on commit 4137671

Please sign in to comment.