sh3b0 / cplus Goto Github PK
View Code? Open in Web Editor NEWCompiler for an imperative toy programming language
License: MIT License
Compiler for an imperative toy programming language
License: MIT License
routine main(x: integer) : integer is
println x;
return 0;
end
$ ./cplus -d source.cp
$ ./a.out 5
Prints 5
Prints other random value, although the generated ir.ll
seems correct.
routine modify(x : integer) is
x := 2;
println x;
return;
end
routine main() is
modify(5);
return;
end
Prints 2
[AST]:
<Program>
| <RoutineDeclaration>
| | <Body>
| | | <AssignmentStatement>
| | | | <Identifier>
| | | | </Identifier>
cplus: /usr/lib/llvm-10/include/llvm/IR/Type.h:382: llvm::Type* llvm::Type::getPointerElementType() const: Assertion `getTypeID() == PointerTyID' failed.
./run.sh: line 5: 73279 Aborted (core dumped) ./build/cplus -d examples/sample.cp
tmp_v
rather than tmp_p
in cast of argument (from args_table)routine main() is
var x : array[5] integer;
x[-1] := -1;
x[0] := 0;
x[6] := 6;
println x[-1];
println x[0];
println x[6];
return;
end
Runtime errors as we access invalid indexes (btw, C+ arrays are 1-indexed)
Prints -1, 0, 6
Although seems cool, it shouldn't be like this.
routine main() is
var x : array[5] integer;
x[1] := 1;
var y is x[1];
println y;
return;
end
Prints 1
Prints garbage from memory, most probably the error originates from the local variable intialization code in VariableDeclarartion::visit
routine main() is
var x : integer is 1.1;
println x;
return;
end
Prints 1 as x is casted from real to integer.
Prints 1.1, x is declared with real dtype as the code deduces dtype
from iv
disregarding user explicit type specification.
Can be fixed by modifying the visit method for VariableDeclaration to use casting as it's already done in AssignmentStatement.
routine power(number : integer, to_power : integer) : integer is
var tmp is 1;
if to_power < 1 then
return 1;
end
for i in 0 .. to_power - 1 loop
tmp := tmp * number;
end
return tmp;
end
routine main(input : integer) is
println power(2, 3);
return;
end
Prints 8
[LLVM]: [WARNINGS]:
Terminator found in the middle of a basic block!
label %then
ir.ll:38:3: error: instruction expected to be numbered '%1'
%0 = load i64, i64* %tmp
^
1 error generated.
Error generating IR
is_first_routine
hack.A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.