Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* terminals used */
- var num, id_var, id_func, id_idx, binaryOP, quote, assignOP;
- /* non-terminals used */
- var Program, Assertion, Expr, TableExpr, Constant, FunctionDef, ColumnDef, Stmt, Assign, Condition, Call, Loop;
- // non terminal class
- function NonTerminal(name) {
- this.name = name;
- this.rules = [];
- this.addRule = function (rule) {
- this.rules.push(rule);
- };
- this.getRandomRule = function () {
- if (this.rules.length == 0) {
- return "";
- }
- return getRandomArrayElement(this.rules);
- };
- this.derive = function () {
- console.log("derive: " + this.name);
- return (this.getRandomRule())();
- };
- }
- // terminal class
- function Terminal(name, deriveFunc) {
- this.name = name;
- this.derive = deriveFunc;
- };
- /* --------- TERMINALS --------- */
- /* Number */
- num = new Terminal("Num", function () {
- return Math.round(Math.random() * 10);
- });
- /* ID */
- id_var = new Terminal("ID", function () {
- return getRandomArrayElement(varIds);
- });
- id_func = new Terminal("ID", function () {
- return getRandomArrayElement(funcIds);
- });
- id_idx = new Terminal("ID", function () {
- return getRandomArrayElement(idxIds);
- });
- /* OP */
- binaryOP = new Terminal("OP", function () {
- if($scope.nonAssoc){
- return getRandomArrayElement(binaryOpsWithNonAssoc);
- }
- else{
- return getRandomArrayElement(binaryOps);
- }
- });
- /* QUOTE */
- quote = new Terminal("QUOTE", function () {
- return getRandomArrayElement(quotes);
- });
- /* ASSIGN OP */
- assignOP = new Terminal("assignOP", function () {
- return getRandomArrayElement(assignOps);
- });
- /* --------- NON-TERMINALS --------- */
- /* Program */
- Program = new NonTerminal("Program");
- Program.addRule(function () {
- return Assertion.derive();
- });
- Program.addRule(function () {
- return Constant.derive();
- });
- Program.addRule(function () {
- return FunctionDef.derive();
- });
- Program.addRule(function () {
- return ColumnDef.derive();
- });
- /* Assertion */
- Assertion = new NonTerminal("Assertion");
- Assertion.addRule(function () {
- return "assert " + Expr.derive() + " : " + quote.derive();
- });
- /* TableExpr */
- TableExpr = new NonTerminal("TableExpr");
- TableExpr.addRule(function () {
- return "COLS";
- });
- TableExpr.addRule(function () {
- return "ROWS";
- });
- TableExpr.addRule(function () {
- return "TABLE[" + Expr.derive() + "][" + Expr.derive() + "]";
- });
- /* Expr */
- Expr = new NonTerminal("Expr");
- Expr.addRule(function () {
- return num.derive();
- });
- Expr.addRule(function () {
- return id_var.derive();
- });
- Expr.addRule(function () {
- return Expr.derive() + " " + binaryOP.derive() + " " + Expr.derive();
- });
- Expr.addRule(function () {
- return "-" + Expr.derive();
- });
- Expr.addRule(function () {
- return "( " + Expr.derive() + " )";
- });
- Expr.addRule(function () {
- return TableExpr.derive();
- });
- Expr.addRule(function () {
- return Call.derive();
- });
- /* Stmt */
- Stmt = new NonTerminal("Stmt");
- Stmt.addRule(function () {
- return Expr.derive()
- });
- Stmt.addRule(function () {
- return Assign.derive()
- });
- Stmt.addRule(function () {
- return Condition.derive()
- });
- //Stmt.addRule(function () { return Loop.derive() });
- /* Constant */
- Constant = new NonTerminal("Constant");
- Constant.addRule(function () {
- return "var " + id_var.derive() + " = " + Expr.derive();
- });
- /* FunctionDef */
- FunctionDef = new NonTerminal("FunctionDef");
- FunctionDef.addRule(function () {
- return "def " + id_func.derive() + "(" + makeList(id_var, ",", false) + "){\n" + makeList(Stmt, ";", true) + "\n}"
- });
- /* ColumnDef */
- ColumnDef = new NonTerminal("ColumnDef");
- ColumnDef.addRule(function () {
- return "def TABLE[" + Expr.derive() + "][" + id_idx.derive() + "] {\n" + makeList(Stmt, ";", true) + "\n}"
- });
- /* Assign */
- Assign = new NonTerminal("Assign");
- Assign.addRule(function () {
- return id_var.derive() + " " + assignOP.derive() + " " + Expr.derive();
- });
- /* Condition */
- Condition = new NonTerminal("Condition");
- Condition.addRule(function () {
- return "if(" + Expr.derive() + "){\n" + makeList(Stmt, ";", true) + "\n}";
- });
- Condition.addRule(function () {
- return "if(" + Expr.derive() + "){\n" + makeList(Stmt, ";", true) + "\n} else {\n" + makeList(Stmt, ";", true) + "\n}";
- });
- /* Call */
- Call = new NonTerminal("Call");
- Call.addRule(function () {
- return id_func.derive() + "(" + makeList(Expr, ",", false) + ")";
- });
- /* Loop */
- Loop = new NonTerminal("Loop");
- Loop.addRule(function () {
- return "for ( " + id_idx.derive() + " = " + Expr.derive() + ".." + Expr.derive() + " ) {\n" + makeList(Stmt, ";", true) + "\n}";
- });
- /* Utils */
- // get a random element from an array
- function getRandomArrayElement(arr) {
- return arr[Math.floor(Math.random() * arr.length)];
- }
- // create a list of 0+ elements
- function makeList(item, seperator, isBody) {
- var listSize = Math.round(Math.random() * MAX_LIST_SIZE);
- var res = "";
- for (var i = 0; i < listSize; i++) {
- if (isBody) {
- res += "\t";
- }
- res += item.derive();
- if (i != listSize - 1) {
- res += seperator;
- if (isBody) {
- res += "\n";
- }
- }
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement