master^2
ldXiao 3 years ago
parent b745aab547
commit 4d80343a57

@ -0,0 +1,24 @@
# Tests for PA3: ChocoPy Code Generation
Clone a student repository:
```
TEAM="team"
git clone --branch=pa3final \
https://github.com/nyu-compiler-construction/pa3-chocopy-code-generation-${TEAM}
```
Build it:
```
cd pa3-chocopy-code-generation-${TEAM}
mvn clean package
```
Clone the tests _into_ the student repository:
```
git clone https://github.com/nyu-compiler-construction/pa3-tests
```
Run the tests:
```
./pa3-tests/run-tests.sh
```

@ -0,0 +1,25 @@
# Compute x**y
def exp(x: int, y: int) -> int:
a: int = 0
def f(i: int) -> int:
nonlocal a
def geta() -> int:
return a
if i <= 0:
return geta()
else:
a = a * x
return f(i-1)
a = 1
return f(y)
# Input parameter
n:int = 42
# Run [0, n]
i:int = 0
# Crunch
while i <= n:
print(exp(2, i % 31))
i = i + 1

@ -0,0 +1,562 @@
{
"kind" : "Program",
"location" : [ 2, 1, 26, 1 ],
"declarations" : [ {
"kind" : "FuncDef",
"location" : [ 2, 1, 14, 13 ],
"name" : {
"kind" : "Identifier",
"location" : [ 2, 5, 2, 7 ],
"name" : "exp"
},
"params" : [ {
"kind" : "TypedVar",
"location" : [ 2, 9, 2, 14 ],
"identifier" : {
"kind" : "Identifier",
"location" : [ 2, 9, 2, 9 ],
"name" : "x"
},
"type" : {
"kind" : "ClassType",
"location" : [ 2, 12, 2, 14 ],
"className" : "int"
}
}, {
"kind" : "TypedVar",
"location" : [ 2, 17, 2, 22 ],
"identifier" : {
"kind" : "Identifier",
"location" : [ 2, 17, 2, 17 ],
"name" : "y"
},
"type" : {
"kind" : "ClassType",
"location" : [ 2, 20, 2, 22 ],
"className" : "int"
}
} ],
"returnType" : {
"kind" : "ClassType",
"location" : [ 2, 28, 2, 30 ],
"className" : "int"
},
"declarations" : [ {
"kind" : "VarDef",
"location" : [ 3, 2, 3, 11 ],
"var" : {
"kind" : "TypedVar",
"location" : [ 3, 2, 3, 7 ],
"identifier" : {
"kind" : "Identifier",
"location" : [ 3, 2, 3, 2 ],
"name" : "a"
},
"type" : {
"kind" : "ClassType",
"location" : [ 3, 5, 3, 7 ],
"className" : "int"
}
},
"value" : {
"kind" : "IntegerLiteral",
"location" : [ 3, 11, 3, 11 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 0
}
}, {
"kind" : "FuncDef",
"location" : [ 4, 2, 13, 1 ],
"name" : {
"kind" : "Identifier",
"location" : [ 4, 6, 4, 6 ],
"name" : "f"
},
"params" : [ {
"kind" : "TypedVar",
"location" : [ 4, 8, 4, 13 ],
"identifier" : {
"kind" : "Identifier",
"location" : [ 4, 8, 4, 8 ],
"name" : "i"
},
"type" : {
"kind" : "ClassType",
"location" : [ 4, 11, 4, 13 ],
"className" : "int"
}
} ],
"returnType" : {
"kind" : "ClassType",
"location" : [ 4, 19, 4, 21 ],
"className" : "int"
},
"declarations" : [ {
"kind" : "NonLocalDecl",
"location" : [ 5, 3, 5, 12 ],
"variable" : {
"kind" : "Identifier",
"location" : [ 5, 12, 5, 12 ],
"name" : "a"
}
}, {
"kind" : "FuncDef",
"location" : [ 6, 3, 7, 12 ],
"name" : {
"kind" : "Identifier",
"location" : [ 6, 7, 6, 10 ],
"name" : "geta"
},
"params" : [ ],
"returnType" : {
"kind" : "ClassType",
"location" : [ 6, 17, 6, 19 ],
"className" : "int"
},
"declarations" : [ ],
"statements" : [ {
"kind" : "ReturnStmt",
"location" : [ 7, 4, 7, 11 ],
"value" : {
"kind" : "Identifier",
"location" : [ 7, 11, 7, 11 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "a"
}
} ]
} ],
"statements" : [ {
"kind" : "IfStmt",
"location" : [ 8, 3, 13, 1 ],
"condition" : {
"kind" : "BinaryExpr",
"location" : [ 8, 6, 8, 11 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "bool"
},
"left" : {
"kind" : "Identifier",
"location" : [ 8, 6, 8, 6 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "i"
},
"operator" : "<=",
"right" : {
"kind" : "IntegerLiteral",
"location" : [ 8, 11, 8, 11 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 0
}
},
"thenBody" : [ {
"kind" : "ReturnStmt",
"location" : [ 9, 4, 9, 16 ],
"value" : {
"kind" : "CallExpr",
"location" : [ 9, 11, 9, 16 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"function" : {
"kind" : "Identifier",
"location" : [ 9, 11, 9, 14 ],
"inferredType" : {
"kind" : "FuncType",
"parameters" : [ ],
"returnType" : {
"kind" : "ClassValueType",
"className" : "int"
}
},
"name" : "geta"
},
"args" : [ ]
}
} ],
"elseBody" : [ {
"kind" : "AssignStmt",
"location" : [ 11, 4, 11, 12 ],
"targets" : [ {
"kind" : "Identifier",
"location" : [ 11, 4, 11, 4 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "a"
} ],
"value" : {
"kind" : "BinaryExpr",
"location" : [ 11, 8, 11, 12 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"left" : {
"kind" : "Identifier",
"location" : [ 11, 8, 11, 8 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "a"
},
"operator" : "*",
"right" : {
"kind" : "Identifier",
"location" : [ 11, 12, 11, 12 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "x"
}
}
}, {
"kind" : "ReturnStmt",
"location" : [ 12, 4, 12, 16 ],
"value" : {
"kind" : "CallExpr",
"location" : [ 12, 11, 12, 16 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"function" : {
"kind" : "Identifier",
"location" : [ 12, 11, 12, 11 ],
"inferredType" : {
"kind" : "FuncType",
"parameters" : [ {
"kind" : "ClassValueType",
"className" : "int"
} ],
"returnType" : {
"kind" : "ClassValueType",
"className" : "int"
}
},
"name" : "f"
},
"args" : [ {
"kind" : "BinaryExpr",
"location" : [ 12, 13, 12, 15 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"left" : {
"kind" : "Identifier",
"location" : [ 12, 13, 12, 13 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "i"
},
"operator" : "-",
"right" : {
"kind" : "IntegerLiteral",
"location" : [ 12, 15, 12, 15 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 1
}
} ]
}
} ]
} ]
} ],
"statements" : [ {
"kind" : "AssignStmt",
"location" : [ 13, 2, 13, 6 ],
"targets" : [ {
"kind" : "Identifier",
"location" : [ 13, 2, 13, 2 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "a"
} ],
"value" : {
"kind" : "IntegerLiteral",
"location" : [ 13, 6, 13, 6 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 1
}
}, {
"kind" : "ReturnStmt",
"location" : [ 14, 2, 14, 12 ],
"value" : {
"kind" : "CallExpr",
"location" : [ 14, 9, 14, 12 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"function" : {
"kind" : "Identifier",
"location" : [ 14, 9, 14, 9 ],
"inferredType" : {
"kind" : "FuncType",
"parameters" : [ {
"kind" : "ClassValueType",
"className" : "int"
} ],
"returnType" : {
"kind" : "ClassValueType",
"className" : "int"
}
},
"name" : "f"
},
"args" : [ {
"kind" : "Identifier",
"location" : [ 14, 11, 14, 11 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "y"
} ]
}
} ]
}, {
"kind" : "VarDef",
"location" : [ 17, 1, 17, 10 ],
"var" : {
"kind" : "TypedVar",
"location" : [ 17, 1, 17, 5 ],
"identifier" : {
"kind" : "Identifier",
"location" : [ 17, 1, 17, 1 ],
"name" : "n"
},
"type" : {
"kind" : "ClassType",
"location" : [ 17, 3, 17, 5 ],
"className" : "int"
}
},
"value" : {
"kind" : "IntegerLiteral",
"location" : [ 17, 9, 17, 10 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 42
}
}, {
"kind" : "VarDef",
"location" : [ 20, 1, 20, 9 ],
"var" : {
"kind" : "TypedVar",
"location" : [ 20, 1, 20, 5 ],
"identifier" : {
"kind" : "Identifier",
"location" : [ 20, 1, 20, 1 ],
"name" : "i"
},
"type" : {
"kind" : "ClassType",
"location" : [ 20, 3, 20, 5 ],
"className" : "int"
}
},
"value" : {
"kind" : "IntegerLiteral",
"location" : [ 20, 9, 20, 9 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 0
}
} ],
"statements" : [ {
"kind" : "WhileStmt",
"location" : [ 23, 1, 26, 1 ],
"condition" : {
"kind" : "BinaryExpr",
"location" : [ 23, 7, 23, 12 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "bool"
},
"left" : {
"kind" : "Identifier",
"location" : [ 23, 7, 23, 7 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "i"
},
"operator" : "<=",
"right" : {
"kind" : "Identifier",
"location" : [ 23, 12, 23, 12 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "n"
}
},
"body" : [ {
"kind" : "ExprStmt",
"location" : [ 24, 2, 24, 22 ],
"expr" : {
"kind" : "CallExpr",
"location" : [ 24, 2, 24, 22 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "<None>"
},
"function" : {
"kind" : "Identifier",
"location" : [ 24, 2, 24, 6 ],
"inferredType" : {
"kind" : "FuncType",
"parameters" : [ {
"kind" : "ClassValueType",
"className" : "object"
} ],
"returnType" : {
"kind" : "ClassValueType",
"className" : "<None>"
}
},
"name" : "print"
},
"args" : [ {
"kind" : "CallExpr",
"location" : [ 24, 8, 24, 21 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"function" : {
"kind" : "Identifier",
"location" : [ 24, 8, 24, 10 ],
"inferredType" : {
"kind" : "FuncType",
"parameters" : [ {
"kind" : "ClassValueType",
"className" : "int"
}, {
"kind" : "ClassValueType",
"className" : "int"
} ],
"returnType" : {
"kind" : "ClassValueType",
"className" : "int"
}
},
"name" : "exp"
},
"args" : [ {
"kind" : "IntegerLiteral",
"location" : [ 24, 12, 24, 12 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 2
}, {
"kind" : "BinaryExpr",
"location" : [ 24, 15, 24, 20 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"left" : {
"kind" : "Identifier",
"location" : [ 24, 15, 24, 15 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "i"
},
"operator" : "%",
"right" : {
"kind" : "IntegerLiteral",
"location" : [ 24, 19, 24, 20 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 31
}
} ]
} ]
}
}, {
"kind" : "AssignStmt",
"location" : [ 25, 2, 25, 10 ],
"targets" : [ {
"kind" : "Identifier",
"location" : [ 25, 2, 25, 2 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "i"
} ],
"value" : {
"kind" : "BinaryExpr",
"location" : [ 25, 6, 25, 10 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"left" : {
"kind" : "Identifier",
"location" : [ 25, 6, 25, 6 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "i"
},
"operator" : "+",
"right" : {
"kind" : "IntegerLiteral",
"location" : [ 25, 10, 25, 10 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 1
}
}
} ]
} ],
"errors" : {
"errors" : [ ],
"kind" : "Errors",
"location" : [ 0, 0, 0, 0 ]
}
}

@ -0,0 +1,43 @@
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
1
2
4
8
16
32
64
128
256
512
1024
2048

@ -0,0 +1,30 @@
# Get the n-th prime starting from 2
def get_prime(n:int) -> int:
candidate:int = 2
found:int = 0
while True:
if is_prime(candidate):
found = found + 1
if found == n:
return candidate
candidate = candidate + 1
return 0 # Never happens
def is_prime(x:int) -> bool:
div:int = 2
while div < x:
if x % div == 0:
return False
div = div + 1
return True
# Input parameter
n:int = 15
# Run [1, n]
i:int = 1
# Crunch
while i <= n:
print(get_prime(i))
i = i + 1

@ -0,0 +1,658 @@
{
"kind" : "Program",
"location" : [ 2, 1, 31, 1 ],
"declarations" : [ {
"kind" : "FuncDef",
"location" : [ 2, 1, 11, 29 ],
"name" : {
"kind" : "Identifier",
"location" : [ 2, 5, 2, 13 ],
"name" : "get_prime"
},
"params" : [ {
"kind" : "TypedVar",
"location" : [ 2, 15, 2, 19 ],
"identifier" : {
"kind" : "Identifier",
"location" : [ 2, 15, 2, 15 ],
"name" : "n"
},
"type" : {
"kind" : "ClassType",
"location" : [ 2, 17, 2, 19 ],
"className" : "int"
}
} ],
"returnType" : {
"kind" : "ClassType",
"location" : [ 2, 25, 2, 27 ],
"className" : "int"
},
"declarations" : [ {
"kind" : "VarDef",
"location" : [ 3, 5, 3, 21 ],
"var" : {
"kind" : "TypedVar",
"location" : [ 3, 5, 3, 17 ],
"identifier" : {
"kind" : "Identifier",
"location" : [ 3, 5, 3, 13 ],
"name" : "candidate"
},
"type" : {
"kind" : "ClassType",
"location" : [ 3, 15, 3, 17 ],
"className" : "int"
}
},
"value" : {
"kind" : "IntegerLiteral",
"location" : [ 3, 21, 3, 21 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 2
}
}, {
"kind" : "VarDef",
"location" : [ 4, 5, 4, 17 ],
"var" : {
"kind" : "TypedVar",
"location" : [ 4, 5, 4, 13 ],
"identifier" : {
"kind" : "Identifier",
"location" : [ 4, 5, 4, 9 ],
"name" : "found"
},
"type" : {
"kind" : "ClassType",
"location" : [ 4, 11, 4, 13 ],
"className" : "int"
}
},
"value" : {
"kind" : "IntegerLiteral",
"location" : [ 4, 17, 4, 17 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 0
}
} ],
"statements" : [ {
"kind" : "WhileStmt",
"location" : [ 5, 5, 11, 4 ],
"condition" : {
"kind" : "BooleanLiteral",
"location" : [ 5, 11, 5, 14 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "bool"
},
"value" : true
},
"body" : [ {
"kind" : "IfStmt",
"location" : [ 6, 9, 10, 8 ],
"condition" : {
"kind" : "CallExpr",
"location" : [ 6, 12, 6, 30 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "bool"
},
"function" : {
"kind" : "Identifier",
"location" : [ 6, 12, 6, 19 ],
"inferredType" : {
"kind" : "FuncType",
"parameters" : [ {
"kind" : "ClassValueType",
"className" : "int"
} ],
"returnType" : {
"kind" : "ClassValueType",
"className" : "bool"
}
},
"name" : "is_prime"
},
"args" : [ {
"kind" : "Identifier",
"location" : [ 6, 21, 6, 29 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "candidate"
} ]
},
"thenBody" : [ {
"kind" : "AssignStmt",
"location" : [ 7, 13, 7, 29 ],
"targets" : [ {
"kind" : "Identifier",
"location" : [ 7, 13, 7, 17 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "found"
} ],
"value" : {
"kind" : "BinaryExpr",
"location" : [ 7, 21, 7, 29 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"left" : {
"kind" : "Identifier",
"location" : [ 7, 21, 7, 25 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "found"
},
"operator" : "+",
"right" : {
"kind" : "IntegerLiteral",
"location" : [ 7, 29, 7, 29 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 1
}
}
}, {
"kind" : "IfStmt",
"location" : [ 8, 13, 10, 8 ],
"condition" : {
"kind" : "BinaryExpr",
"location" : [ 8, 16, 8, 25 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "bool"
},
"left" : {
"kind" : "Identifier",
"location" : [ 8, 16, 8, 20 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "found"
},
"operator" : "==",
"right" : {
"kind" : "Identifier",
"location" : [ 8, 25, 8, 25 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "n"
}
},
"thenBody" : [ {
"kind" : "ReturnStmt",
"location" : [ 9, 17, 9, 32 ],
"value" : {
"kind" : "Identifier",
"location" : [ 9, 24, 9, 32 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "candidate"
}
} ],
"elseBody" : [ ]
} ],
"elseBody" : [ ]
}, {
"kind" : "AssignStmt",
"location" : [ 10, 9, 10, 33 ],
"targets" : [ {
"kind" : "Identifier",
"location" : [ 10, 9, 10, 17 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "candidate"
} ],
"value" : {
"kind" : "BinaryExpr",
"location" : [ 10, 21, 10, 33 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"left" : {
"kind" : "Identifier",
"location" : [ 10, 21, 10, 29 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "candidate"
},
"operator" : "+",
"right" : {
"kind" : "IntegerLiteral",
"location" : [ 10, 33, 10, 33 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 1
}
}
} ]
}, {
"kind" : "ReturnStmt",
"location" : [ 11, 5, 11, 12 ],
"value" : {
"kind" : "IntegerLiteral",
"location" : [ 11, 12, 11, 12 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 0
}
} ]
}, {
"kind" : "FuncDef",
"location" : [ 13, 1, 19, 16 ],
"name" : {
"kind" : "Identifier",
"location" : [ 13, 5, 13, 12 ],
"name" : "is_prime"
},
"params" : [ {
"kind" : "TypedVar",
"location" : [ 13, 14, 13, 18 ],
"identifier" : {
"kind" : "Identifier",
"location" : [ 13, 14, 13, 14 ],
"name" : "x"
},
"type" : {
"kind" : "ClassType",
"location" : [ 13, 16, 13, 18 ],
"className" : "int"
}
} ],
"returnType" : {
"kind" : "ClassType",
"location" : [ 13, 24, 13, 27 ],
"className" : "bool"
},
"declarations" : [ {
"kind" : "VarDef",
"location" : [ 14, 5, 14, 15 ],
"var" : {
"kind" : "TypedVar",
"location" : [ 14, 5, 14, 11 ],
"identifier" : {
"kind" : "Identifier",
"location" : [ 14, 5, 14, 7 ],
"name" : "div"
},
"type" : {
"kind" : "ClassType",
"location" : [ 14, 9, 14, 11 ],
"className" : "int"
}
},
"value" : {
"kind" : "IntegerLiteral",
"location" : [ 14, 15, 14, 15 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 2
}
} ],
"statements" : [ {
"kind" : "WhileStmt",
"location" : [ 15, 5, 19, 4 ],
"condition" : {
"kind" : "BinaryExpr",
"location" : [ 15, 11, 15, 17 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "bool"
},
"left" : {
"kind" : "Identifier",
"location" : [ 15, 11, 15, 13 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "div"
},
"operator" : "<",
"right" : {
"kind" : "Identifier",
"location" : [ 15, 17, 15, 17 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "x"
}
},
"body" : [ {
"kind" : "IfStmt",
"location" : [ 16, 9, 18, 8 ],
"condition" : {
"kind" : "BinaryExpr",
"location" : [ 16, 12, 16, 23 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "bool"
},
"left" : {
"kind" : "BinaryExpr",
"location" : [ 16, 12, 16, 18 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"left" : {
"kind" : "Identifier",
"location" : [ 16, 12, 16, 12 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "x"
},
"operator" : "%",
"right" : {
"kind" : "Identifier",
"location" : [ 16, 16, 16, 18 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "div"
}
},
"operator" : "==",
"right" : {
"kind" : "IntegerLiteral",
"location" : [ 16, 23, 16, 23 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 0
}
},
"thenBody" : [ {
"kind" : "ReturnStmt",
"location" : [ 17, 13, 17, 24 ],
"value" : {
"kind" : "BooleanLiteral",
"location" : [ 17, 20, 17, 24 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "bool"
},
"value" : false
}
} ],
"elseBody" : [ ]
}, {
"kind" : "AssignStmt",
"location" : [ 18, 9, 18, 21 ],
"targets" : [ {
"kind" : "Identifier",
"location" : [ 18, 9, 18, 11 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "div"
} ],
"value" : {
"kind" : "BinaryExpr",
"location" : [ 18, 15, 18, 21 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"left" : {
"kind" : "Identifier",
"location" : [ 18, 15, 18, 17 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "div"
},
"operator" : "+",
"right" : {
"kind" : "IntegerLiteral",
"location" : [ 18, 21, 18, 21 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 1
}
}
} ]
}, {
"kind" : "ReturnStmt",
"location" : [ 19, 5, 19, 15 ],
"value" : {
"kind" : "BooleanLiteral",
"location" : [ 19, 12, 19, 15 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "bool"
},
"value" : true
}
} ]
}, {
"kind" : "VarDef",
"location" : [ 22, 1, 22, 10 ],
"var" : {
"kind" : "TypedVar",
"location" : [ 22, 1, 22, 5 ],
"identifier" : {
"kind" : "Identifier",
"location" : [ 22, 1, 22, 1 ],
"name" : "n"
},
"type" : {
"kind" : "ClassType",
"location" : [ 22, 3, 22, 5 ],
"className" : "int"
}
},
"value" : {
"kind" : "IntegerLiteral",
"location" : [ 22, 9, 22, 10 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 15
}
}, {
"kind" : "VarDef",
"location" : [ 25, 1, 25, 9 ],
"var" : {
"kind" : "TypedVar",
"location" : [ 25, 1, 25, 5 ],
"identifier" : {
"kind" : "Identifier",
"location" : [ 25, 1, 25, 1 ],
"name" : "i"
},
"type" : {
"kind" : "ClassType",
"location" : [ 25, 3, 25, 5 ],
"className" : "int"
}
},
"value" : {
"kind" : "IntegerLiteral",
"location" : [ 25, 9, 25, 9 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 1
}
} ],
"statements" : [ {
"kind" : "WhileStmt",
"location" : [ 28, 1, 31, 1 ],
"condition" : {
"kind" : "BinaryExpr",
"location" : [ 28, 7, 28, 12 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "bool"
},
"left" : {
"kind" : "Identifier",
"location" : [ 28, 7, 28, 7 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "i"
},
"operator" : "<=",
"right" : {
"kind" : "Identifier",
"location" : [ 28, 12, 28, 12 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "n"
}
},
"body" : [ {
"kind" : "ExprStmt",
"location" : [ 29, 5, 29, 23 ],
"expr" : {
"kind" : "CallExpr",
"location" : [ 29, 5, 29, 23 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "<None>"
},
"function" : {
"kind" : "Identifier",
"location" : [ 29, 5, 29, 9 ],
"inferredType" : {
"kind" : "FuncType",
"parameters" : [ {
"kind" : "ClassValueType",
"className" : "object"
} ],
"returnType" : {
"kind" : "ClassValueType",
"className" : "<None>"
}
},
"name" : "print"
},
"args" : [ {
"kind" : "CallExpr",
"location" : [ 29, 11, 29, 22 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"function" : {
"kind" : "Identifier",
"location" : [ 29, 11, 29, 19 ],
"inferredType" : {
"kind" : "FuncType",
"parameters" : [ {
"kind" : "ClassValueType",
"className" : "int"
} ],
"returnType" : {
"kind" : "ClassValueType",
"className" : "int"
}
},
"name" : "get_prime"
},
"args" : [ {
"kind" : "Identifier",
"location" : [ 29, 21, 29, 21 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "i"
} ]
} ]
}
}, {
"kind" : "AssignStmt",
"location" : [ 30, 5, 30, 13 ],
"targets" : [ {
"kind" : "Identifier",
"location" : [ 30, 5, 30, 5 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "i"
} ],
"value" : {
"kind" : "BinaryExpr",
"location" : [ 30, 9, 30, 13 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"left" : {
"kind" : "Identifier",
"location" : [ 30, 9, 30, 9 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"name" : "i"
},
"operator" : "+",
"right" : {
"kind" : "IntegerLiteral",
"location" : [ 30, 13, 30, 13 ],
"inferredType" : {
"kind" : "ClassValueType",
"className" : "int"
},
"value" : 1
}
}
} ]
} ],
"errors" : {
"errors" : [ ],
"kind" : "Errors",
"location" : [ 0, 0, 0, 0 ]
}
}

@ -0,0 +1,15 @@
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47

@ -0,0 +1,107 @@
# A resizable list of integers
class Vector(object):
items: [int] = None
size: int = 0
def __init__(self:"Vector"):
self.items = [0]
# Returns current capacity
def capacity(self:"Vector") -> int:
return len(self.items)
# Increases capacity of vector by one element
def increase_capacity(self:"Vector") -> int:
self.items = self.items + [0]
return self.capacity()
# Appends one item to end of vector
def append(self:"Vector", item: int) -> object:
if self.size == self.capacity():
self.increase_capacity()
self.items[self.size] = item
self.size = self.size + 1
# Appends many items to end of vector
def append_all(self:"Vector", new_items: [int]) -> object:
item:int = 0
for item in new_items:
self.append(item)
# Removes an item from the middle of vector
def remove_at(self:"Vector", idx: int) -> object:
if idx < 0:
return
while idx < self.size - 1:
self.items[idx] = self.items[idx + 1]
idx = idx + 1
self.size = self.size - 1
# Retrieves an item at a given index
def get(self:"Vector", idx: int) -> int:
return self.items[idx]
# Retrieves the current size of the vector
def length(self:"Vector") -> int:
return self.size
# A faster (but more memory-consuming) implementation of vector
class DoublingVector(Vector):
doubling_limit:int = 1000
# Overriding to do fewer resizes
def increase_capacity(self:"DoublingVector") -> int:
if (self.capacity() <= self.doubling_limit // 2):
self.items = self.items + self.items
else:
# If doubling limit has been reached, fall back to
# standard capacity increases
self.items = self.items + [0]
return self.capacity()
# Makes a vector in the range [i, j)
def vrange(i:int, j:int) -> Vector:
v:Vector = None
v = DoublingVector()
while i < j:
v.append(i)
i = i + 1
return v
# Sieve of Eratosthenes (not really)
def sieve(v:Vector) -> object:
i:int = 0
j:int = 0
k:int = 0
while i < v.length():
k = v.get(i)
j = i + 1
while j < v.length():
if v.get(j) % k == 0:
v.remove_at(j)
else:
j = j + 1
i = i + 1
# Input parameter
n:int = 50
# Data
v:Vector = None
i:int = 0
# Crunch
v = vrange(2, n)
sieve(v)
# Print
while i < v.length():
print(v.get(i))
i = i + 1

File diff suppressed because it is too large Load Diff

@ -0,0 +1,15 @@
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47

@ -0,0 +1,77 @@
# ChocoPy library functions
def int_to_str(x: int) -> str:
digits:[str] = None
result:str = ""
# Set-up digit mapping
digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
# Write sign if necessary
if x < 0:
result = "-"
x = -x
# Write digits using a recursive call
if x >= 10:
result = result + int_to_str(x // 10)
result = result + digits[x % 10]
return result
def str_to_int(x: str) -> int:
result:int = 0
digit:int = 0
char:str = ""
sign:int = 1
first_char:bool = True
# Parse digits
for char in x:
if char == "-":
if not first_char:
return 0 # Error
sign = -1
elif char == "0":
digit = 0
elif char == "1":
digit = 1
elif char == "2":
digit = 2
elif char == "3":
digit = 3
elif char == "3":
digit = 3
elif char == "4":
digit = 4
elif char == "5":
digit = 5
elif char == "6":
digit = 6
elif char == "7":
digit = 7
elif char == "8":
digit = 8
elif char == "9":
digit = 9
else:
return 0 # On error
first_char = False
result = result * 10 + digit
# Compute result
return result * sign
# Input parameters
c:int = 42
n:int = 10
# Run [-nc, nc] with step size c
s:str = ""
i:int = 0
i = -n * c
# Crunch
while i <= n * c:
s = int_to_str(i)
print(s)
i = str_to_int(s) + c

File diff suppressed because it is too large Load Diff

@ -0,0 +1,21 @@
-420
-378
-336
-294
-252
-210
-168
-126
-84
-42
0
42
84
126
168
210
252
294
336
378
420

@ -0,0 +1,83 @@
# Binary-search trees
class TreeNode(object):
value:int = 0
left:"TreeNode" = None
right:"TreeNode" = None
def insert(self:"TreeNode", x:int) -> bool:
if x < self.value:
if self.left is None:
self.left = makeNode(x)
return True
else:
return self.left.insert(x)
elif x > self.value:
if self.right is None:
self.right = makeNode(x)
return True
else:
return self.right.insert(x)
return False
def contains(self:"TreeNode", x:int) -> bool:
if x < self.value:
if self.left is None:
return False
else:
return self.left.contains(x)
elif x > self.value:
if self.right is None:
return False
else:
return self.right.contains(x)
else:
return True
class Tree(object):
root:TreeNode = None
size:int = 0
def insert(self:"Tree", x:int) -> object:
if self.root is None:
self.root = makeNode(x)
self.size = 1
else:
if self.root.insert(x):
self.size = self.size + 1
def contains(self:"Tree", x:int) -> bool:
if self.root is None:
return False
else:
return self.root.contains(x)
def makeNode(x: int) -> TreeNode:
b:TreeNode = None
b = TreeNode()
b.value = x
return b
# Input parameters
n:int = 100
c:int = 4
# Data
t:Tree = None
i:int = 0
k:int = 37813
# Crunch
t = Tree()
while i < n:
t.insert(k)
k = (k * 37813) % 37831
if i % c != 0:
t.insert(i)
i = i + 1
print(t.size)
for i in [4, 8, 15, 16, 23, 42]:
if t.contains(i):
print(i)

File diff suppressed because it is too large Load Diff

Binary file not shown.

@ -0,0 +1,17 @@
def f() -> int:
print("start f")
g()
print("end f")
return 42
def g() -> object:
print("start g")
h()
print("end g")
def h() -> object:
print("start h")
print("end h")
print(f())