Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b1e3555

Browse filesBrowse files
committed
1.1.0
1 parent a4a0108 commit b1e3555
Copy full SHA for b1e3555

File tree

Expand file treeCollapse file tree

25 files changed

+325
-8
lines changed
Filter options
Expand file treeCollapse file tree

25 files changed

+325
-8
lines changed

‎1.1.0/ast/syntax.json

Copy file name to clipboardExpand all lines: 1.1.0/ast/syntax.json
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
"type": "WHITESPACE",
1010
"parser": null
1111
},
12+
{
13+
"type": "IMPORT",
14+
"parser": "./syntax/imports"
15+
},
16+
{
17+
"type": "CLASS",
18+
"parser": "./syntax/class"
19+
},
20+
{
21+
"type": "CLASS_INSTANCE",
22+
"parser": "./syntax/class"
23+
},
1224
{
1325
"type": "FUNCTION",
1426
"parser": "./syntax/fun"

‎1.1.0/ast/syntax/class.js

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const { log, err, warn, info, json, success } = require('../../helpers/logger');
2+
3+
module.exports = (peek, consume, peekBack, recall, nextLine, parse) => {
4+
console.log(peek());
5+
if (peek().type == "CLASS") {
6+
// Class creation
7+
consume();
8+
if (peek().type != "ID")
9+
err(`Expected 'ID' but got '${consume().type}'`);
10+
let className = consume().value;
11+
console.log(peek());
12+
if (peek().type != "BRACE_OPEN")
13+
err(`Expected '{' but got '${consume().type}'`);
14+
consume();
15+
let code = [];
16+
do {
17+
let parsed = parse(peek());
18+
if (parsed)
19+
code.push(parsed);
20+
} while (peek().type != "BRACE_CLOSE");
21+
consume();
22+
return {
23+
type: 'CLASS',
24+
name: className,
25+
code: code
26+
};
27+
} else {
28+
// New class instance
29+
consume();
30+
if (peek().type != "ID")
31+
err(`Expected 'ID' but got '${consume().type}'`);
32+
let initializer = parse(peek());
33+
console.log(peek());
34+
return {
35+
type: 'CLASS_INSTANCE',
36+
name: initializer.function,
37+
args: initializer.args
38+
};
39+
}
40+
}

‎1.1.0/ast/syntax/fun.js

Copy file name to clipboardExpand all lines: 1.1.0/ast/syntax/fun.js
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ module.exports = (peek, consume, peekBack, recall, nextLine, parse) => {
2727
if (peek().type != "TYPE")
2828
err(`Expected type but got '${peek().type}'`);
2929
arg.type = consume().value;
30+
31+
if ((peek()||{type:null}).type == "ASSIGN") {
32+
consume();
33+
arg.default = parse(peek());
34+
}
35+
3036
fun.args.push(arg);
37+
if ((peek()||{type:null}).type == "COMMA")
38+
consume();
3139
} while (peek().type != "PAREN_CLOSE");
3240
}
3341
consume();

‎1.1.0/ast/syntax/imports.js

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { log, err, warn, info, json, success } = require('../../helpers/logger');
2+
const { resolve } = require('path');
3+
const fs = require('fs');
4+
5+
module.exports = (peek, consume, peekBack, recall, nextLine, parse) => {
6+
consume();
7+
let path = peek().value.substr(1, peek().value.length - 2);
8+
if (!path.includes('.saur')) {
9+
// It's a built-in library:
10+
path = resolve(__dirname, '../../lib/', path + '.saur');
11+
if (!fs.existsSync(path)) {
12+
err(`The library '${peek().value}' cannot be found. Did you mean to reference a local '.saur' file?`);
13+
}
14+
}
15+
consume();
16+
return {
17+
type: "IMPORT",
18+
path: path
19+
}
20+
}

‎1.1.0/ast/syntax/repeat.js

Copy file name to clipboardExpand all lines: 1.1.0/ast/syntax/repeat.js
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ module.exports = (peek, consume, peekBack, recall, nextLine, parse) => {
3131
consume();
3232
} while ((peek()||{type:null}).type == "NEWLINE");
3333
consume();
34+
do {
35+
consume();
36+
} while ((peek()||{type:null}).type == "NEWLINE");
3437

3538
// We can iterate an array by using `with`. This is the equivalent to doing `for (let item in array)`
3639
if ((peek()||{type:null}).type == "REPEAT_WITH") {

‎1.1.0/helpers/logger.js

Copy file name to clipboardExpand all lines: 1.1.0/helpers/logger.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ function timestamp() {
55
let date = new Date();
66
return '['.gray + ((date.getHours() % 12) + ':' + date.getMinutes() + ':' + date.getSeconds()).magenta + ']'.gray;
77
}*/
8-
prefix = "[".gray + "saur".magenta + "]".gray;
8+
prefix = "[" + "saur".gray + "]";
99

1010
module.exports = {
1111
setPrefix: function(str, intensity) {
1212
let colored = intensity == 0 ? str.yellow : intensity == 1 ? str.green : str.green.bold;
13-
prefix = "[".gray + colored + "]".gray;
13+
prefix = "[" + str.gray + "]";
1414
},
1515
log: function(msg) {
1616
console.log(prefix, msg);

‎1.1.0/lexer/rules.json

Copy file name to clipboardExpand all lines: 1.1.0/lexer/rules.json
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,30 @@
99
"match": "^(\n|\\;)",
1010
"type": "NEWLINE"
1111
},
12+
{
13+
"match": "^\\/\\/.*",
14+
"type": "COMMENT"
15+
},
1216
{
1317
"match": "^\\s+",
1418
"type": "WHITESPACE"
1519
},
20+
{
21+
"match": "^\\@import",
22+
"type": "IMPORT"
23+
},
24+
{
25+
"match": "^class",
26+
"type": "CLASS"
27+
},
28+
{
29+
"match": "^new",
30+
"type": "CLASS_INSTANCE"
31+
},
32+
{
33+
"match": "^loop",
34+
"type": "MAIN_LOOP"
35+
},
1636
{
1737
"match": "^fun",
1838
"type": "FUNCTION"

‎1.1.0/lib/browser.saur

Copy file name to clipboard
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@import "./browser/page.saur"
2+
@import "./browser/storage.saur"
3+
4+
@export Page
5+
6+
storage: Storage = new Storage()
7+
@export storage

‎1.1.0/lib/browser/http.saur

Copy file name to clipboard
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fun HttpGET(url: String, callback: Any) {
2+
request: Any = new XMLHttpRequest();
3+
request.addEventListener("load", callback)
4+
request.open("GET", url)
5+
request.send()
6+
}

‎1.1.0/lib/browser/page.saur

Copy file name to clipboard
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Page {
2+
// The HTML code
3+
html: String
4+
5+
// Template is an array of tuples (a dictionary of sort)
6+
// It looks like this:
7+
// [["title", "Hello World"], ["description", "This is an example app"]]
8+
// This will look for any {{title}} and {{description}} tags in the HTML and replace it
9+
fun render(template: Any, soFar: String = "", i: Int = 0) > String {
10+
if (i < template.length) {
11+
replaced: String = html.replace(template[i][0], tempalte[i][1])
12+
return render(template, replaced, i + 1)
13+
}
14+
}
15+
}

‎1.1.0/lib/browser/storage.saur

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Storage {
2+
fun getItem(key: String) > String {
3+
return localStorage.getItem(key)
4+
}
5+
fun setItem(key: String, value: String) {
6+
localStorage.setItem(key, value)
7+
}
8+
}

‎1.1.0/lib/common.saur

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fun println(message: Any) {
2+
console.log(message)
3+
}

‎1.1.0/transpiler/transpiler.js

Copy file name to clipboardExpand all lines: 1.1.0/transpiler/transpiler.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Transpiler {
2323
if (parserFile.transpiler) {
2424
const parser = require(parserFile.transpiler);
2525
code += parser(statement);
26+
code += '\n';
2627
} else {
2728
err(`No transpiler found for statement of type '${statement.type}'`);
2829
}

‎1.1.0/transpiler/transpilers.json

Copy file name to clipboardExpand all lines: 1.1.0/transpiler/transpilers.json
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
{
22
"version": "1.1.0",
33
"transpilers": [
4+
{
5+
"type": "IMPORT",
6+
"transpiler": "./transpilers/import.js"
7+
},
48
{
59
"type": "NEW_VARIABLE",
610
"transpiler": "./transpilers/var.js"
711
},
12+
{
13+
"type": "CLASS",
14+
"transpiler": "./transpilers/class.js"
15+
},
16+
{
17+
"type": "CLASS_INSTANCE",
18+
"transpiler": "./transpilers/class.js"
19+
},
820
{
921
"type": "VARIABLE",
1022
"transpiler": "./transpilers/var.js"

‎1.1.0/transpiler/transpilers/call.js

Copy file name to clipboardExpand all lines: 1.1.0/transpiler/transpilers/call.js
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ const Transpiler = require('../transpiler');
33
module.exports = statement => {
44
let args = [];
55
statement.args.forEach(arg => args.push((new Transpiler([arg])).compile()));
6-
return `${statement.function}(${args.join(', ')})`;
6+
let argList = args.map(arg => `(${arg})`);
7+
return `${statement.function}${argList.join('')};`;
78
}

‎1.1.0/transpiler/transpilers/class.js

Copy file name to clipboard
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = statement => {
2+
console.log(statement);
3+
return "\n";
4+
}

‎1.1.0/transpiler/transpilers/fun.js

Copy file name to clipboardExpand all lines: 1.1.0/transpiler/transpilers/fun.js
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ const Transpiler = require('../transpiler');
22

33
module.exports = statement => {
44
let args = [];
5-
statement.args.forEach(arg => args.push(arg.name));
5+
statement.args.forEach(arg => {
6+
if (arg.default)
7+
args.push(`${arg.name} = ${(new Transpiler([arg.default])).compile()}`);
8+
else
9+
args.push(arg.name);
10+
});
611
let code = (new Transpiler(statement.code)).compile();
7-
return `function ${statement.name}(${args.join(', ')}) { ${code} }`;
12+
// We use fat arrow syntax for currying later:
13+
return `const ${statement.name} = (${args.join(') => (')}) => { ${code} }`;
814
}
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const Lexer = require('../../lexer/lexer');
2+
const AST = require('../../ast/ast');
3+
const Transpiler = require('../transpiler');
4+
5+
const fs = require('fs');
6+
7+
module.exports = statement => {
8+
let file = statement.path;
9+
let code = fs.readFileSync(file, 'utf8');
10+
11+
let lex = new Lexer(code);
12+
let tree = new AST(lex.lex());
13+
14+
return `
15+
/* --- ${file} --- */
16+
${(new Transpiler(tree.tree)).compile()}
17+
18+
`;
19+
}

‎cli/index.js

Copy file name to clipboardExpand all lines: cli/index.js
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const ora = require('ora');
99

1010
const defaultCompiler = '1.1.0';
1111

12+
const appLoop = `if (appLoop) { let loopParams = []; setInterval(_ => { appLoop().apply(this, loopParams); }, 0.1); }`;
13+
1214
function error(msg) {
1315
console.log(msg.red.bold);
1416
process.exit();
@@ -69,8 +71,8 @@ function build(file, outFile, compiler) {
6971
info("Read \`.saur\' file");
7072
let lex = new Lexer(code);
7173
let tree = new AST(lex.lex());
72-
let js = new Transpiler(tree);
73-
fs.writeFileSync(outFile, js.compile());
74+
let js = new Transpiler(tree.tree);
75+
fs.writeFileSync(outFile, js.compile() + appLoop);
7476
success("Saved to " + outFile);
7577
}
7678

‎example.js

Copy file name to clipboardExpand all lines: example.js
+5-1Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎examples/classTest.saur

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@import "common"
2+
3+
class MyClass {
4+
fun init() {
5+
return [["property", 100], ["property2", 200]]
6+
}
7+
}
8+
9+
fun appLoop() {
10+
11+
}

‎examples/compiled/classTest.js

Copy file name to clipboard
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
/* --- /Users/Ryan/Desktop/LazyScript/1.1.0/lib/common.saur --- */
3+
const println = (message) => { console.log(message
4+
);
5+
}
6+
7+
8+
9+
10+
11+
const appLoop = () => { }
12+
if (appLoop) { let loopParams = []; setInterval(_ => { appLoop().apply(this, loopParams); }, 0.1); }

‎examples/compiled/todos.js

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
/* --- /Users/Ryan/Desktop/LazyScript/1.1.0/lib/common.saur --- */
3+
const println = (message) => { console.log(message
4+
);
5+
}
6+
7+
8+
9+
const makeTitle = (todos) => { return todos.length
10+
+ " Todos\n----------"
11+
12+
;
13+
}
14+
const drawTodos = (title) => (todos) => { println(title
15+
);
16+
for (let i = 0; i < todos.length
17+
; i++) { println(todos[i
18+
]
19+
);
20+
};
21+
println("\n\n"
22+
);
23+
}
24+
const appLoop = (data = ["Task 1"
25+
, "Task 2"
26+
, "Task 3"
27+
]
28+
) => (draw = drawTodos
29+
) => { const title = makeTitle(data
30+
);
31+
;
32+
draw(title
33+
)(data
34+
);
35+
return [data
36+
, draw
37+
]
38+
;
39+
}
40+
if (appLoop) { let loopParams = []; setInterval(_ => { appLoop().apply(this, loopParams); }, 0.1); }

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.