Comments (4)
See it here https://github.com/mingodad/CocoR-Typescript
from cocor.
I just add an online playground here https://mingodad.github.io/CocoR-Typescript/playground
from cocor.
Hi mingodad
Actually, my last commit is half a decade ago, I switched away from MS tools, and moreover I'm using ANTLR4 now b/c it's more "present" in terms of supporting articles on the internet compared to CocoR. This version here adds some features that I called enhancements over the original 2011 version. I'm not at all sure if the original authors would like these. So it's probably a good idea to clearly state if you ported the original code or the one containing my extras.
However, I consider it a great idea to have a TS/JS based version of CocoR b/c it greatly extends the usability of the tool.
So, with your kind permission, I'd put some links to your repositories to the readme, alongside with the supported feature set.
KR
Martin
Trying the playground with my old examples
- https://github.com/Lercher/CocoR/blob/singhammer-wfmodel/CocoRCore/sample-grammars/Taste/Taste.ATG (Original sample) -> lots of errors.
- https://github.com/Lercher/CocoR/blob/master/CSharp/Coco.atg -> lots of errors. It's long ago but I guess that's b/c of the interleaved C# code
- https://github.com/Lercher/CocoR/blob/singhammer-wfmodel/CSharp/test/inheritance.atg -> lists 2 errors at USEONCE/USEALL, so it's probably the 2011 version.
- https://github.com/Lercher/CocoR/blob/singhammer-wfmodel/CSharp/WFModel/WFModel.atg -> Hangs
from cocor.
Hello Martin !
Thanks for reply !
The errors are due to the difference in type declaration between CSharp and Typescript and of course the CSharp code that need to be converted to Javascript to work on the playground.
Try the CSharp
example that I converted to Javascript to work on the playground (with the select
on top middle screen);
Also noticed that the erros
are clickable and the editor jump to the offending line.
Here is the Taste.ATG
with the types fixed that does parse fine the grammar :
//$namespace=CocoRCore.Samples.Taste
COMPILER Taste
const int // types
undef = 0, integer = 1, boolean = 2;
const int // object kinds
var = 0, proc = 1;
public SymbolTable tab;
public CodeGenerator gen;
/*--------------------------------------------------------------------------*/
CHARACTERS
letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜabcdefghijklmnopqrstuvwxyzäöüß".
digit = "0123456789".
cr = '\r'.
lf = '\n'.
tab = '\t'.
TOKENS
ident = letter {letter | digit}.
number = digit {digit}.
COMMENTS FROM "/*" TO "*/" NESTED
COMMENTS FROM "//" TO lf
COMMENTS FROM "#" TO lf
IGNORE cr + lf + tab
PRODUCTIONS
/*------------------------------------------------------------------------*/
AddOp<out op : Op>
= (. op = Op.ADD; .)
( '+'
| '-' (. op = Op.SUB; .)
).
/*------------------------------------------------------------------------*/
Expr<out type: int> (. int type1; Op op; .)
= SimExpr<out type>
[ RelOp<out op>
SimExpr<out type1> (. if (type != type1) SemErr(1, "incompatible types");
gen.Emit(op); type = boolean; .)
].
/*------------------------------------------------------------------------*/
Factor<out type: int> (. int n; Obj obj; string name; .)
= (. type = undef; .)
( Ident<out name> (. obj = tab.Find(name); type = obj.type;
if (obj.kind == var) {
if (obj.level == 0) gen.Emit(Op.LOADG, obj.adr);
else gen.Emit(Op.LOAD, obj.adr);
} else SemErr(2, "variable expected"); .)
| number (. n = Convert.ToInt32(t.val);
gen.Emit(Op.CONST, n); type = integer; .)
| '-'
Factor<out type> (. if (type != integer) {
SemErr(3, "integer type expected"); type = integer;
}
gen.Emit(Op.NEG); .)
| "true" (. gen.Emit(Op.CONST, 1); type = boolean; .)
| "false" (. gen.Emit(Op.CONST, 0); type = boolean; .)
).
/*------------------------------------------------------------------------*/
Ident<out name : string>
= ident (. name = t.val; .).
/*------------------------------------------------------------------------*/
MulOp<out op : Op>
= (. op = Op.MUL; .)
( '*'
| '/' (. op = Op.DIV; .)
).
/*------------------------------------------------------------------------*/
ProcDecl (. string name; Obj obj; int adr; .)
= "void"
Ident<out name> (. obj = tab.NewObj(name, proc, undef); obj.adr = gen.pc;
if (name == "Main") gen.progStart = gen.pc;
tab.OpenScope(); .)
'(' ')'
'{' (. gen.Emit(Op.ENTER, 0); adr = gen.pc - 2; .)
{ VarDecl | Stat }
'}' (. gen.Emit(Op.LEAVE); gen.Emit(Op.RET);
gen.Patch(adr, tab.topScope.nextAdr);
tab.CloseScope(); .).
/*------------------------------------------------------------------------*/
RelOp<out op : Op>
= (. op = Op.EQU; .)
( "=="
| '<' (. op = Op.LSS; .)
| '>' (. op = Op.GTR; .)
).
/*------------------------------------------------------------------------*/
SimExpr<out type : int> (. int type1; Op op; .)
= Term<out type>
{ AddOp<out op>
Term<out type1> (. if (type != integer || type1 != integer)
SemErr(4, "integer type expected");
gen.Emit(op); .)
}.
/*------------------------------------------------------------------------*/
Stat (. int type; string name; Obj obj;
int adr, adr2, loopstart; .)
= Ident<out name> (. obj = tab.Find(name); .)
( '=' (. if (obj.kind != var) SemErr(5, "cannot assign to procedure"); .)
Expr<out type> ';'
(. if (type != obj.type) SemErr(6, "incompatible types");
if (obj.level == 0) gen.Emit(Op.STOG, obj.adr);
else gen.Emit(Op.STO, obj.adr); .)
| '(' ')' ';' (. if (obj.kind != proc) SemErr(7, "object is not a procedure");
gen.Emit(Op.CALL, obj.adr); .)
)
| "if"
'(' Expr<out type> ')' (. if (type != boolean) SemErr(8, "boolean type expected");
gen.Emit(Op.FJMP, 0); adr = gen.pc - 2; .)
Stat
[ "else" (. gen.Emit(Op.JMP, 0); adr2 = gen.pc - 2;
gen.Patch(adr, gen.pc);
adr = adr2; .)
Stat
] (. gen.Patch(adr, gen.pc); .)
| "while" (. loopstart = gen.pc; .)
'(' Expr<out type> ')' (. if (type != boolean) SemErr(9, "boolean type expected");
gen.Emit(Op.FJMP, 0); adr = gen.pc - 2; .)
Stat (. gen.Emit(Op.JMP, loopstart); gen.Patch(adr, gen.pc); .)
| "read"
Ident<out name> ';' (. obj = tab.Find(name);
if (obj.type != integer) SemErr(10, "integer type expected");
gen.Emit(Op.READ);
if (obj.level == 0) gen.Emit(Op.STOG, obj.adr);
else gen.Emit(Op.STO, obj.adr); .)
| "write"
Expr<out type> ';' (. if (type != integer) SemErr(11, "integer type expected");
gen.Emit(Op.WRITE); .)
| '{' { Stat | VarDecl } '}' .
/*------------------------------------------------------------------------*/
Taste (. string name; .)
= "program"
Ident<out name> (. tab.OpenScope(); .)
'{'
{ VarDecl | ProcDecl }
'}' (. tab.CloseScope();
if (gen.progStart == -1) SemErr(12, "main function never defined");
.).
/*------------------------------------------------------------------------*/
Term<out type : int> (. int type1; Op op; .)
= Factor<out type>
{ MulOp<out op>
Factor<out type1> (. if (type != integer || type1 != integer)
SemErr(13, "integer type expected");
gen.Emit(op); .)
}.
/*------------------------------------------------------------------------*/
Type<out type : int>
= (. type = undef; .)
( "int" (. type = integer; .)
| "bool" (. type = boolean; .)
).
/*------------------------------------------------------------------------*/
VarDecl (. string name; int type; .)
= Type<out type>
Ident<out name> (. tab.NewObj(name, var, type); .)
{ ',' Ident<out name> (. tab.NewObj(name, var, type); .)
} ';'.
END Taste.
Cheers !
from cocor.
Related Issues (2)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cocor.