Skip to content

Commit

Permalink
Empiezo el compilador
Browse files Browse the repository at this point in the history
  • Loading branch information
pauek committed Oct 8, 2011
1 parent eae0976 commit 3d04f6a
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 17 deletions.
3 changes: 3 additions & 0 deletions compiler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
c++-parser.js
c++.js
examples
118 changes: 118 additions & 0 deletions compiler/ast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@

var ast = ast || {};

/* Model of inheritance */

ast.__inherit__ = (function() {
var F = function (typename) {
this.typename = typename;
};
return function (typename, C, P) {
F.prototype = P.prototype;
C.prototype = new F(typename);
C.uber = P.prototype;
C.prototype.constructor = C;
}
})();

/* Node type */

ast.Node = function () {};

ast.Node.prototype = {
typename: "Node",
accept: function (visitor) {
var methodName = 'v' + this.typename;
if (methodName in visitor) {
visitor[methodName].call(visitor, this);
} else if ('vNode' in visitor) {
visitor['vNode'].call(visitor, this);
}
},
};

ast.NodeList = function() {
this.children = [];
}

ast.__inherit__("NodeList", ast.NodeList, ast.Node);

ast.NodeList.prototype.add = function (obj) {
this.children.push(obj);
}


/* Construct node types */

function nodeTypeMaker(Base) {
return function (typename) {
var NewType = function (obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
this[prop] = obj[prop];
}
}
}
ast.__inherit__(typename, NewType, Base);
ast[typename] = NewType;
}
}

ast.makeNodeType = nodeTypeMaker(ast.Node);

/* Create Node Types */


var types = [
"IncludeDirective",
"UsingDirective",
"ForStmt",
"WhileStmt",
];
for (var i in types) {
ast.makeNodeType(types[i]);
}

/* Visitors */

ast.reportVisitor = {
vNode: function(obj) {
console.log("seen a Node!");
},
vIncludeDirective: function (obj) {
console.log("seen an Include!");
},
vNodeList: function (obj) {
console.log("seen a NodeList!");
for (var i in obj.children) {
obj.children[i].accept(this);
}
}
}

ast.nodeCount = function () {
this.count = 0;
}

ast.nodeCount.prototype = {
vNode: function(obj) {
this.count++;
}
}

/* test... */

var util = require('util');

var i = new ast.IncludeDirective({ name: "iostream" });
var n = new ast.Node();
var L = new ast.NodeList();
L.add(i);
L.add(n);
console.log(util.inspect(L, true, 4))

L.accept(ast.reportVisitor);

var nc = new ast.nodeCount();
i.accept(nc);
console.log(nc.count);
123 changes: 123 additions & 0 deletions compiler/c++.pegjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@

start =
__ program:Program __ { return program; }

WhiteSpace = [ \t\f]
LineTerminator = [\n\r]
LineTerminatorSeq = "\n" / "\r\n" / "\r"
SourceCharacter = .

__ =
(WhiteSpace / LineTerminatorSeq / Comment)*

_ =
(WhiteSpace / MultiLineCommentOneLine / SingleLineComment)*

Literal = [0-9]

MultiLineComment =
"/*" (!"*/" SourceCharacter)* "*/"

MultiLineCommentOneLine =
"/*" (!("*/" / LineTerminator) SourceCharacter)* "*/"

SingleLineComment =
"//" (!LineTerminator SourceCharacter)*

Comment =
MultiLineComment /
SingleLineComment

Identifier =
name:([_A-Za-z] [_A-Za-z0-9]*) {
return new ast.Identifier(name);
}

Type =
"int" /
"char" /
"float"

FormalParameter =
type:Type _ name:Identifier {
return {
type: type,
name: name
}
}

FormalParameterList =
head:FormalParameter tail:(__ "," __ FormalParameter)* {
var result = [head];
for (var i = 0; i < tail.length; i++) {
result.push(tail[i][3]);
}
return result;
}

VarDecl =
name:Identifier (__ "=" __ value:Literal)? {
return {
name: name,
value: value,
}
}

VarDeclList =
head:VarDecl tail:(__ "," __ VarDecl) {
var result = [head];
for (var i = 0; i < tail.length; i++) {
result.push(tail[i][3]);
}
return result;
}

VarDeclStmt =
type:Type __ list:VarDeclList {
return new ast.VarDeclStmt(type, list);
}

Stmt =
VarDeclStmt

StmtList =
head:Stmt tail:(__ Stmt)* {
var result = [head];
for (var i = 0; i < tail.length; i++) {
result.push(tail[i][1]);
}
return result;
}


FunctionBody =
StmtList

FunctionDef =
Type _ name:Identifier
"(" __ params:FormalParameterList? __ ")" __
"{" __ elements:FunctionBody? __ "}" {
return new ast.Function(name, params, elements);
}

IncludeDirective "include" =
"#include" _ [<"] file:[a-z]* [>"] {
console.log("include!");
return new ast.Include(file)
}

ProgramPart =
IncludeDirective /
FunctionDef /
Comment

Program =
head:ProgramPart tail:(__ ProgramPart)* {
var result = [head];
for (var i = 0; i < tail.length; i++) {
result.push(tail[i][1]);
}
return new ast.Program(result);
}


11 changes: 11 additions & 0 deletions compiler/cc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env node

var util = require('util');
var fs = require('fs');
var cc = require('c++-parser');

fs.readFile('test.cc', 'utf-8', function (err, data) {
var tree = cc.parse(data);
console.log(util.inspect(tree, true, null));
});

3 changes: 3 additions & 0 deletions compiler/gen_parser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
pegjs c++.pegjs c++.js
cat preamble.js c++.js > c++-parser.js
3 changes: 3 additions & 0 deletions compiler/preamble.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

var ast = require('ast');

4 changes: 4 additions & 0 deletions compiler/test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <iostream>

int main() {
}
36 changes: 19 additions & 17 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
<html>
<head>
<script src="/dnode.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript" ></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js" type="text/javascript" ></script>
<script src="/client.js" type="text/javascript"></script>
<script src="/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="/mode-c_cpp.js" type="text/javascript" charset="utf-8"></script>

<link href="css/Aristo/jquery-ui-1.8.7.custom.css" type="text/css" rel="stylesheet" />
<link href="css/webcc.css" type="text/css" rel="stylesheet" />
<script src="/dnode.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"
type="text/javascript" ></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js"
type="text/javascript" ></script>
<script src="/client.js" type="text/javascript"></script>
<script src="/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="/mode-c_cpp.js" type="text/javascript" charset="utf-8"></script>

<link href="css/Aristo/jquery-ui-1.8.7.custom.css" type="text/css" rel="stylesheet" />
<link href="css/webcc.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="barra">
<div id="compileButton">Compila</div>
<div id="runButton">Executa</div>
<div id="settingsButton">Preferències</div>
</div>
<div id="editor" /></div>
<div id="errors" /></div>
<div id="barra">
<div id="compileButton">Compila</div>
<div id="runButton">Executa</div>
<div id="settingsButton">Preferències</div>
</div>
<div id="editor" /></div>
<div id="errors" /></div>
<script type="text/editor" id="program">
#include <iostream>
using namespace std;
Expand All @@ -32,5 +34,5 @@
Indentació <br />
Tamany de la font <br />
</div>
</body>
</body>
</html>

0 comments on commit 3d04f6a

Please sign in to comment.