Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
INF222 - V24 - Obligatory 2 - Skeleton
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
V.Larsen
INF222 - V24 - Obligatory 2 - Skeleton
Commits
b82d76e4
Commit
b82d76e4
authored
11 months ago
by
Vetle Larsen
Browse files
Options
Downloads
Patches
Plain Diff
Finished Oblig 2
parent
19864632
Branches
master
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
index.ts
+1
-3
1 addition, 3 deletions
index.ts
src/evaluateVisitor.ts
+11
-8
11 additions, 8 deletions
src/evaluateVisitor.ts
src/parser.ts
+10
-10
10 additions, 10 deletions
src/parser.ts
src/typeCheckVisitor.ts
+6
-4
6 additions, 4 deletions
src/typeCheckVisitor.ts
with
28 additions
and
25 deletions
index.ts
+
1
−
3
View file @
b82d76e4
...
@@ -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];
This diff is collapsed.
Click to expand it.
src/evaluateVisitor.ts
+
11
−
8
View file @
b82d76e4
...
@@ -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.66
6
7
,
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.
277
778
,
value
:
node
.
numericalValue
*
0.
0002
778
,
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.0166
66
7
,
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
{
...
...
This diff is collapsed.
Click to expand it.
src/parser.ts
+
10
−
10
View file @
b82d76e4
...
@@ -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
();
...
...
This diff is collapsed.
Click to expand it.
src/typeCheckVisitor.ts
+
6
−
4
View file @
b82d76e4
...
@@ -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!
"
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment