Skip to content
Snippets Groups Projects
Commit b82d76e4 authored by Vetle Larsen's avatar Vetle Larsen
Browse files

Finished Oblig 2

parent 19864632
Branches master
No related tags found
No related merge requests found
...@@ -19,6 +19,4 @@ function main() { ...@@ -19,6 +19,4 @@ function main() {
console.log(varName, result.value, result.unit.value); console.log(varName, result.value, result.unit.value);
} }
} }
main(); main();
\ No newline at end of file
//let blabla = b / f + g + 1[km/s];
...@@ -55,7 +55,7 @@ export class EvaluateVisitor implements AbstractVisitor { ...@@ -55,7 +55,7 @@ export class EvaluateVisitor implements AbstractVisitor {
// 1.3.1. Cast the node to type `GroupExpr`. // 1.3.1. Cast the node to type `GroupExpr`.
node = node as GroupExpr; node = node as GroupExpr;
// 1.3.2. Return the result of visiting the child `subExpr` of the node. // 1.3.2. Return the result of visiting the child `subExpr` of the node.
this.visit(node.subExpr); return this.visit(node.subExpr);
// 1.4. If it is a measured number, then: // 1.4. If it is a measured number, then:
case "MeasuredNumber": case "MeasuredNumber":
...@@ -107,7 +107,7 @@ export class EvaluateVisitor implements AbstractVisitor { ...@@ -107,7 +107,7 @@ export class EvaluateVisitor implements AbstractVisitor {
} }
case "km/h": case "km/h":
return { return {
value: node.numericalValue/3.6, value: node.numericalValue*0.2778,
unit: { ...node.unit, value: "m/s" }, unit: { ...node.unit, value: "m/s" },
} }
case "km/s": case "km/s":
...@@ -117,12 +117,12 @@ export class EvaluateVisitor implements AbstractVisitor { ...@@ -117,12 +117,12 @@ export class EvaluateVisitor implements AbstractVisitor {
} }
case "km/min": case "km/min":
return { return {
value: node.numericalValue*16.6667, value: node.numericalValue*16.667,
unit: { ...node.unit, value: "m/s" }, unit: { ...node.unit, value: "m/s" },
} }
case "m/h": case "m/h":
return { return {
value: node.numericalValue*0.277778, value: node.numericalValue*0.0002778,
unit: { ...node.unit, value: "m/s"}, unit: { ...node.unit, value: "m/s"},
} }
case "m/s": case "m/s":
...@@ -132,7 +132,7 @@ export class EvaluateVisitor implements AbstractVisitor { ...@@ -132,7 +132,7 @@ export class EvaluateVisitor implements AbstractVisitor {
} }
case "m/min": case "m/min":
return { return {
value: node.numericalValue*0.0166667, value: node.numericalValue*0.01667,
unit: { ...node.unit, value: "m/s" }, unit: { ...node.unit, value: "m/s" },
} }
// 1.4.2.14. Otherwise: // 1.4.2.14. Otherwise:
...@@ -164,7 +164,7 @@ export class EvaluateVisitor implements AbstractVisitor { ...@@ -164,7 +164,7 @@ export class EvaluateVisitor implements AbstractVisitor {
} else if (node.op.value === "-") { } else if (node.op.value === "-") {
return { return {
value: left.value - right.value, value: left.value - right.value,
unit: left.unit, unit: left.unit,
}; };
} }
// 1.5.4.3. Otherwise, the operation should be prohibited, and an error is reported. // 1.5.4.3. Otherwise, the operation should be prohibited, and an error is reported.
...@@ -182,13 +182,16 @@ export class EvaluateVisitor implements AbstractVisitor { ...@@ -182,13 +182,16 @@ export class EvaluateVisitor implements AbstractVisitor {
if (node.op.value === "*") { if (node.op.value === "*") {
return { return {
value: leftNode.value * rightNode.value, value: leftNode.value * rightNode.value,
unit: leftNode.unit, unit: leftNode.unit
}; };
} }
else if (node.op.value === "/") { else if (node.op.value === "/") {
if (rightNode.value === 0) {
throw new Error("Division by zero");
}
return { return {
value: leftNode.value / rightNode.value, value: leftNode.value / rightNode.value,
unit: leftNode.unit, unit: leftNode.unit
}; };
} }
else { else {
......
...@@ -164,7 +164,7 @@ export function parseProgram(tokens: Token[]): AstNode[] { ...@@ -164,7 +164,7 @@ export function parseProgram(tokens: Token[]): AstNode[] {
nodeType: "PhysicalUnit" nodeType: "PhysicalUnit"
}; };
// 3.1.3. Otherwise, if that value is one of the mass units, then: // 3.1.3. Otherwise, if that value is one of the mass units, then:
} else if (["g", "kg"]) { } else if (["g", "kg"].includes(unitValue)) {
// 3.1.3.1. Return an object with the following properties: // 3.1.3.1. Return an object with the following properties:
return { return {
// 3.1.3.1.1. Property `value` should be the value of the token, casted to type `Mass` // 3.1.3.1.1. Property `value` should be the value of the token, casted to type `Mass`
...@@ -176,7 +176,7 @@ export function parseProgram(tokens: Token[]): AstNode[] { ...@@ -176,7 +176,7 @@ export function parseProgram(tokens: Token[]): AstNode[] {
}; };
// 3.1.4. Otherwise, if that value is one of the (???what???) units, then: // 3.1.4. Otherwise, if that value is one of the (???what???) units, then:
} else if (["m", "km"]) { } else if (["m", "km"].includes(unitValue)) {
return { return {
value: unitValue as Distance, value: unitValue as Distance,
kind: PhysicalUnitEnum.Distance, kind: PhysicalUnitEnum.Distance,
...@@ -184,7 +184,7 @@ export function parseProgram(tokens: Token[]): AstNode[] { ...@@ -184,7 +184,7 @@ export function parseProgram(tokens: Token[]): AstNode[] {
} }
} }
// 3.1.5. Otherwise, if that value is one of the (???what???) units, then: // 3.1.5. Otherwise, if that value is one of the (???what???) units, then:
else if (["km/h", "km/s", "km/min", "m/h", "m/s", "m/min"]) { else if (["km/h", "km/s", "km/min", "m/h", "m/s", "m/min"].includes(unitValue)) {
return { return {
value: unitValue as Velocity, value: unitValue as Velocity,
kind: PhysicalUnitEnum.Velocity, kind: PhysicalUnitEnum.Velocity,
...@@ -222,9 +222,9 @@ export function parseProgram(tokens: Token[]): AstNode[] { ...@@ -222,9 +222,9 @@ export function parseProgram(tokens: Token[]): AstNode[] {
// 6. Return an AST node that represents an assignment statement -- it is an object with: // 6. Return an AST node that represents an assignment statement -- it is an object with:
return { return {
// 6.1. A property that represents the assignee (i.e., the variable that has been assigned to). // 6.1. A property that represents the assignee (i.e., the variable that has been assigned to).
assignee, assignee: assignee,
// 6.2. A property that represents the expression on the right-hand side of the assignment statement. // 6.2. A property that represents the expression on the right-hand side of the assignment statement.
expr, expr: expr,
// 6.3. A property `nodeType` which is (???what???). // 6.3. A property `nodeType` which is (???what???).
nodeType : "AssignmentStatement" nodeType : "AssignmentStatement"
}; };
...@@ -240,7 +240,7 @@ export function parseProgram(tokens: Token[]): AstNode[] { ...@@ -240,7 +240,7 @@ export function parseProgram(tokens: Token[]): AstNode[] {
ClosingBracket(); ClosingBracket();
return { return {
subExpr, subExpr: subExpr,
nodeType: "GroupExpr" nodeType: "GroupExpr"
}; };
} }
...@@ -274,8 +274,8 @@ export function parseProgram(tokens: Token[]): AstNode[] { ...@@ -274,8 +274,8 @@ export function parseProgram(tokens: Token[]): AstNode[] {
let unit = PhysicalUnit(); let unit = PhysicalUnit();
return { return {
numericalValue, numericalValue: numericalValue,
unit, unit: unit,
nodeType: "MeasuredNumber" nodeType: "MeasuredNumber"
}; };
} }
...@@ -355,7 +355,7 @@ export function parseProgram(tokens: Token[]): AstNode[] { ...@@ -355,7 +355,7 @@ export function parseProgram(tokens: Token[]): AstNode[] {
// 3. Expect (i.e., consume) that peek'ed token, and store it in a variable. // 3. Expect (i.e., consume) that peek'ed token, and store it in a variable.
let op = OpAddSub(); let op = OpAddSub();
// 4. Expect a multiplicative expression, and store it in a variable. // 4. Expect a multiplicative expression, and store it in a variable.
let right = Multiplicative(); let right: PrimitiveExpr | Multiplicative = Multiplicative();
// 5. Update the expression stored in item 1. to be an AST node that represents an additive expression -- it is an object with: // 5. Update the expression stored in item 1. to be an AST node that represents an additive expression -- it is an object with:
left = { left = {
// 5.1. A property that represents the left operand of the multiplicative expression. // 5.1. A property that represents the left operand of the multiplicative expression.
...@@ -374,7 +374,7 @@ export function parseProgram(tokens: Token[]): AstNode[] { ...@@ -374,7 +374,7 @@ export function parseProgram(tokens: Token[]): AstNode[] {
} }
function Multiplicative(): Multiplicative | PrimitiveExpr { function Multiplicative(): Multiplicative | PrimitiveExpr {
let left: Multiplicative | PrimitiveExpr = PrimitiveExpr(); //May be a problem let left: PrimitiveExpr | Multiplicative = PrimitiveExpr();
while (peek().kind === "OpMulDiv") { while (peek().kind === "OpMulDiv") {
let op = OpMulDiv(); let op = OpMulDiv();
let right = PrimitiveExpr(); let right = PrimitiveExpr();
......
...@@ -56,7 +56,12 @@ export class TypeCheckVisitor implements AbstractVisitor { ...@@ -56,7 +56,12 @@ export class TypeCheckVisitor implements AbstractVisitor {
// 1.5. If it is an addition-like expression, then: // 1.5. If it is an addition-like expression, then:
case "Additive": { case "Additive": {
node = node as Additive; node = node as Additive;
return this.visit(node.left); let left = this.visit(node.left);
let right = this.visit(node.right);
if (right === left) {
return left;
}
throw new Error("Left and right not alike");
} }
// 1.6. It it is a multiplication-like expression, then: // 1.6. It it is a multiplication-like expression, then:
...@@ -86,9 +91,6 @@ export class TypeCheckVisitor implements AbstractVisitor { ...@@ -86,9 +91,6 @@ export class TypeCheckVisitor implements AbstractVisitor {
if (left === PhysicalUnitEnum.Distance && right === PhysicalUnitEnum.Time) { if (left === PhysicalUnitEnum.Distance && right === PhysicalUnitEnum.Time) {
return PhysicalUnitEnum.Velocity; return PhysicalUnitEnum.Velocity;
} }
if (right === 0) {
throw new Error("Division by zero");
}
} }
// 1.6.4.3. Otherwise, if it any other operator, report an error. // 1.6.4.3. Otherwise, if it any other operator, report an error.
throw new Error("Failure!"); throw new Error("Failure!");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment