All files / src/languages/lambda ast.js

5.45% Statements 3/55
0% Branches 0/9
0% Functions 0/18
5.45% Lines 3/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 1231x 1x 1x                                                                                                                                                                                                                                                
import React from 'react';
import {ASTNode} from '../../ast';
import Node from '../../components/Node';
 
export class Conditional extends ASTNode {
  constructor(from, to, condStatement, thenStatement, elseStatement, options={}) {
    super(from, to, 'conditional', options);
    this.condStatement = condStatement;
    this.thenStatement = thenStatement;
    this.elseStatement = elseStatement;
  }
 
  *[Symbol.iterator]() {
    yield this;
    for (let node of this.condStatement) {
      yield node;
    }
    for (let node of this.thenStatement) {
      yield node;
    }
    if (this.elseStatement) {
      for (let node of this.elseStatement) {
        yield node;
      }
    }
  }
 
  toString() {
    if (!this.elseStatement) {
      return `if (${this.condStatement}) { ${this.thenStatement.join(' ')} }`;
    }
    return `if (${this.condStatement}) { ${this.thenStatement.join(' ')} } else { ${this.body} })`;
  }
}
 
//TODO: add a toString() method
export class Assignment extends ASTNode {
  constructor(from, to, operator, left, right, options={}) {
    super(from, to, 'assignment', options);
    this.operator = operator;
    this.left = left;
    this.right = right;
  }
 
  *[Symbol.iterator]() {
    yield this;
    for (let node of this.left) {
      yield node;
    }
    for (let node of this.right) {
      yield node;
    }
  }
}
 
//TODO: add a toString() method
//is it possible to merge this somehow with assign class? Almost identical with it
export class Binary extends ASTNode {
  constructor(from, to, operator, left, right, options={}) {
    super(from, to, 'binary', options);
    this.operator = operator;
    this.left = left;
    this.right = right;
  }
 
  *[Symbol.iterator]() {
    yield this;
    for (let node of this.left) {
      yield node;
    }
    for (let node of this.right) {
      yield node;
    }
  }
}
 
//TODO: add a toString() method
//use struct toString method as template for this toString() method?
export class Prog extends ASTNode {
  constructor(from, to, prog, options={}) {
    super(from, to, 'prog', options);
    this.prog = prog;
  }
 
  *[Symbol.iterator]() {
    yield this;
    for (let node of this.prog) {
      yield node;
    }
  }
 
  render(props) {
    return (
      <Node node={this} {...props}>
        <h4>Your Lambda Program</h4>
        {this.prog.map((node, index) => (
          <span key={index}>
            {node.reactElement()}
          </span>
        ))}
      </Node>
    );
  }
}
 
//TODO: add a toString() method
export class Let extends ASTNode {
  constructor(from, to, vars, body, options={}) {
    super(from, to, 'let', options);
    this.vars = vars;
    this.body = body;
  }
 
  *[Symbol.iterator]() {
    yield this;
    for (let node of this.vars) {
      yield node;
    }
    //why does body not need a for loop to be iterated over
    yield this.body;
  }
}