Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yosbel Marin committed Oct 24, 2016
2 parents 2aa28a2 + a3b11b1 commit 59050d8
Show file tree
Hide file tree
Showing 20 changed files with 604 additions and 465 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![badge](https://circleci.com/gh/yosbelms/cor/tree/master.png?circle-token=687381b892447ef211dc27dd08a9752b9ec450af)

**The Language of the Web**
**Straightforward language for the Web**

Cor is an opensource language that compiles to regular JavaScript. It is designed to make easy to write simple, fast and maintainable software for the Web.

Expand Down
4 changes: 2 additions & 2 deletions docs/assets/highlightjs/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,11 +782,11 @@ hljs.registerLanguage('cor', function(hljs) {
keyword:
'use class func me return ' +
'if else for in switch case default ' +
'continue break catch go',
'continue break catch go select',
literal:
'true false nil',
built_in:
'super regex error chan timeout'
'super regex error chan timeout copy'
};

return {
Expand Down
64 changes: 25 additions & 39 deletions docs/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,6 @@ go {
}
```

```
// parallel
go {
result = <- (
books : fetch.get('https://api.com/books'),
articles : fetch.get('https://api.com/articles'),
)
for book in result.books {
//..
}
for articles in result.articles {
//..
}
}
```

### Slices/Coalesce Operator/Conditional Operator
```
---
Expand Down Expand Up @@ -650,24 +631,6 @@ go {
}
```

If receiving a array or object of future values it will resolve all values in parallel.
```
go {
result = <- (
books : fetch.get('https://api.com/books'),
articles : fetch.get('https://api.com/articles'),
)
for book in result.books {
//..
}
for articles in result.articles {
//..
}
}
```

Awaiting a coroutine (idiomatic Cor):
```
// get articles in the las hour
Expand Down Expand Up @@ -1179,7 +1142,7 @@ if a < b {

### Switch/Case/Default

With Cor you don't need to remember to `break` after every `case`. Cor switch statements prevents accidental fall-though by automatically breaking at the end of each `case` clause.
With Cor you don't need to remember to `break` after every `case`. Cor switch statements prevents accidental fall-through by automatically breaking at the end of each `case` clause.

Switch statements in Cor can take multiple values for each `case` clause. If any of the values match, the clause runs.
```
Expand Down Expand Up @@ -1208,6 +1171,29 @@ switch {
```


### Select

Select statemet is like `switch` statement but for asynchronic operations. It executes all cases in the same order as defined, once one of them has been resolved, it executes the instructions associated to resolved case. If none of the cases are resolved will block forever.
```
select {
// receive
case <- chan :
// assign receive
case s = <- chan :
// timeout
case timeout(500) :
// default
default :
}
```

If `default` is provided and none of the other operations it will choose to execute the default instructions. The `timeout` builtin function is supported as a special case.


### Inc/Dec

Inc/Dec statement increments or decrements an expression. Similar features are found in many languages but these are offered as an expression, in Cor it is a statement.
Expand Down Expand Up @@ -1570,4 +1556,4 @@ The Cor distribution for browsers is recomended only for development purpose, to

## The CRL (Cor Runtime Library)

The CRL is a small (~13Kb unminified and uncompressed) library which makes possible to take advantage of features such as *for/in statements, inheritance, type assertions, among others*. Without the CRL, the javascript code obtained as the result of compilation could be repetitive and bigger in consequence, that's one of the reasons that CRL exits, to provide a small set of features that will be internally used by the javascript resulting code.
The CRL is a small (~11Kb unminified and uncompressed) library which makes possible to take advantage of features such as *for/in statements, inheritance, type assertions, among others*. Without the CRL, the javascript code obtained as the result of compilation could be repetitive and bigger in consequence, that's one of the reasons that CRL exits, to provide a small set of features that will be internally used by the javascript resulting code.
11 changes: 7 additions & 4 deletions examples/browser/parallel.cor
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ We mock `fetch` API and iterate over future values
that contains the result of each request
---

// inject promise implementation
CRL.Promise = Promise

// append to the body
func print(txt) document.body.innerHTML += $'<pre>{txt}</pre>'

Expand All @@ -21,11 +24,11 @@ fetch = (
func init() go {
print('Parallel Ajax resolution')

for result in <- (
r = <- Promise.all((
fetch.get('github.com'),
fetch.get('twitter.com'),
fetch.get('facebook.com'),
) {
print(result)
}
))

print(r)
}
2 changes: 1 addition & 1 deletion examples/fan_in.cor
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func init() go {
producer(ch, 200)
reader(out)

for (x = <- ch)? {
for x = <- ch {
out <- x
}

Expand Down
2 changes: 1 addition & 1 deletion examples/fan_out.cor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
func worker(ch, name) go {
for (n = <- ch)? {
for n = <- ch {
timeout(n*30)
console.log(n, name)
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"author" : "Yosbel Marin",
"name" : "cor-lang",
"version" : "0.11.1",
"version" : "0.12.0",
"license" : "BSD",
"description" : "The Language of the Web",
"description" : "Straightforward language for the Web",
"repository" : "https://github.com/yosbelms/cor.git",

"preferGlobal": true,
Expand Down
6 changes: 4 additions & 2 deletions src/bnf/cor.l
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[ \f\t\u00A0\u2028\u2029\uFEFF]+ /* skip whitespace */

"//".* %{
yy.env.addComment(
yy.env.addSingleLineComment(
new yy.SingleLineCommentNode(yytext, yylloc)
);
%}
Expand All @@ -29,7 +29,7 @@
throw yy.parseError('comments must end with a new line', {loc: {first_line: yylloc.last_line }}, true);
}

yy.env.addComment(
yy.env.addMultipleLineComment(
new yy.MultiLineCommentNode(yytext, yylloc)
);
%}
Expand Down Expand Up @@ -84,6 +84,7 @@
"for" return 'FOR'
"in" return 'IN'
"switch" return 'SWITCH'
"select" return 'SELECT'
"case" return 'CASE'
"default" return 'DEFAULT'
"continue" return 'CONTINUE'
Expand Down Expand Up @@ -162,6 +163,7 @@ reactiveTerms = {
'FOR' : true,
'IN' : true,
'SWITCH' : true,
'SELECT' : true,
'CASE' : true,
'DEFAULT' : true,
'CONTINUE': true,
Expand Down
37 changes: 36 additions & 1 deletion src/bnf/cor.y
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ ForInRangeStmt
}
;


// switch statement
SwitchStmt
: SWITCH OperationExpr? CaseBlock { $$= new yy.SwitchNode(new yy.Lit($1, @1), $2, $3) }
;
Expand All @@ -284,6 +284,39 @@ CaseStmt
| DEFAULT ':' StrictStmtList { $$= new yy.CaseNode(new yy.Lit($1, @1), new yy.Lit($2, @2), $3) }
;

// race statement
SelectStmt
: SELECT SelectCaseBlock { $$= new yy.SelectNode(new yy.Lit($1, @1), $2) }
;

SelectCaseBlock
: '{' SelectCaseStmtList '}' { $$= new yy.Node(new yy.Lit($1, @1), $2, new yy.Lit($3, @3)) }
;

SelectCaseStmtList
: SelectCaseStmt { $$ = new yy.List($1) }
| SelectCaseStmtList SelectCaseStmt { $1.add($2) }
;

SelectCaseStmt
: CASE Expr ':' StrictStmtList { $$= new yy.SelectCaseNode(new yy.Lit($1, @1), $2, new yy.Lit($3, @3), $4) }
| DEFAULT ':' StrictStmtList {

$$= new yy.SelectCaseNode(
new yy.Lit('case', @1),
new yy.CallNode(
new yy.VarNode(new yy.Lit('timeout', @1)),
new yy.Lit('(', @1),
new yy.List(new yy.Lit('0', @1)),
new yy.Lit(')', @1)
),
new yy.Lit($2, @2),
$3
)

}
;

// Error management
CatchStmt
: CATCH Expr Block { $$= new yy.CatchNode(new yy.Lit($1, @1), $2, $3) }
Expand Down Expand Up @@ -321,6 +354,7 @@ Stmt
| ForInStmt
| ForInRangeStmt
| SwitchStmt
| SelectStmt
| ReturnStmt
| BreakStmt
| ContinueStmt
Expand Down Expand Up @@ -466,6 +500,7 @@ Property
| BOOLEAN
| CATCH
| GO
| SELECT
;

IndexExpr
Expand Down
17 changes: 13 additions & 4 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,22 @@ var Compiler = cor.Class({

compile: function(ast) {
var i,
comments = this.env.getComments(),
len = comments.length;
slComments = this.env.singleLineComments,
mlComments = this.env.multipleLineComments,
slen = slComments.length,
mlen = mlComments.length;

// write multiple line comments first
for (i = 0; i < mlen; i++) {
this.visitNode(mlComments[i]);
}

// write
this.visitNode(ast);

for (i = 0; i < len; i++) {
this.visitNode(comments[i]);
// write single line comments
for (i = 0; i < slen; i++) {
this.visitNode(slComments[i]);
}

return this.generateCode();
Expand Down
Loading

0 comments on commit 59050d8

Please sign in to comment.