Skip to content

Commit

Permalink
Add testsuite/driver for Parser (#10)
Browse files Browse the repository at this point in the history
* Add testsuite/driver for Parser

* Update workflow
  • Loading branch information
XChy committed Aug 4, 2023
1 parent cd5c956 commit 9631f62
Show file tree
Hide file tree
Showing 23 changed files with 395 additions and 34 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Install LLVM
run: sudo apt install llvm-15
run: |
sudo apt remove llvm
sudo apt install llvm-15
- name: CMake Configure
run: cmake .
- name: Build
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ testcases/string
testcases/string.xsharp.bc
testcases/string.xsharp.o
LLVMIR/test/temp
XSharp/test/Parser/temp
22 changes: 22 additions & 0 deletions XSharp/test/Parser/arithmetic.xsharp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
void main()
{
i32 n;
n = inputI32();

i32 i=0;
i32 a=1;
i32 b=1;
i32 c=1;
u8 d = 0;
while (i<n){
a = c = (-a)+b*c/66;
a = b % 3 / 2 + 4 + (b = 4);
b = c;
i=i+1;
}
print(a);
print(b);
print(c);
print(-c);
print(!(c == -1));
}
20 changes: 20 additions & 0 deletions XSharp/test/Parser/arithmetic.xsharp_ast.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
func [main]()->[void] {
Var n:i32
[n] = [call inputI32()]
Var i:i32 = 0
Var a:i32 = 1
Var b:i32 = 1
Var c:i32 = 1
Var d:u8 = 0
while([i] < [n]){
[a] = [[c] = [[-[a]] + [[[b] * [c]] / [66]]]]
[a] = [[[[[b] % [3]] / [2]] + [4]] + [[b] = [4]]]
[b] = [c]
[i] = [[i] + [1]]
}
call print(a)
call print(b)
call print(c)
call print(-[c])
call print(![[c] == [-[1]]])
}
47 changes: 47 additions & 0 deletions XSharp/test/Parser/class.xsharp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Foo{

new(){
self.age = 0;
}

new(i32 a){
self.age = a;
}

void setAge(i32 age){
self.age = age;
}

i32 getAge(){
return self.age;
}

i32 age;

}


i32 main()
{
i64 n = inputI32();

Foo[] c = new Foo[](n);

i64 i = n;
while(i-1>=0)
{
c[i - 1] = new Foo(inputI32());
i = i - 1;
}

double sum = 0;
i = n;
while(i - 1 >= 0){
sum = sum + c[i - 1].getAge();
i = i - 1;
}

print("Average age is:");
print(sum / n);
return 0;
}
33 changes: 33 additions & 0 deletions XSharp/test/Parser/class.xsharp_ast.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Class Foo {
field : constructor(){{
[self.age] = [0]
}}
field : constructor(Var a:i32){{
[self.age] = [a]
}}
field : Var age:i32
field : Foo::func [setAge](Var age:i32)->[void] {
[self.age] = [age]
}
field : Foo::func [getAge]()->[i32] {
return self.age
}
}
func [main]()->[i32] {
Var n:i64 = call inputI32()
Var c:Array<Foo, 1> = call Array<Foo, 1>(n)
Var i:i64 = n
while([[i] - [1]] >= [0]){
[[i] - [1] in c] = [call Foo(call inputI32())]
[i] = [[i] - [1]]
}
Var sum:double = 0
[i] = [n]
while([[i] - [1]] >= [0]){
[sum] = [[sum] + [call [i] - [1] in c.getAge()]]
[i] = [[i] - [1]]
}
call print("Average age is:")
call print([sum] / [n])
return 0
}
5 changes: 5 additions & 0 deletions XSharp/test/Parser/helloworld.xsharp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void main(){
print("Hello,World!");
}


3 changes: 3 additions & 0 deletions XSharp/test/Parser/helloworld.xsharp_ast.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func [main]()->[void] {
call print("Hello,World!")
}
17 changes: 17 additions & 0 deletions XSharp/test/Parser/if.xsharp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

i64 main()
{
i32 i = inputI32();
if(i == 3){
print(0);
if(i > 4){
print(2);
}else{
print(3);
}
}else{
print(1);
}
print(12);
return 10;
}
15 changes: 15 additions & 0 deletions XSharp/test/Parser/if.xsharp_ast.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
func [main]()->[i64] {
Var i:i32 = call inputI32()
If ([i] == [3]) {
call print(0)
If ([i] > [4]) {
call print(2)
} Else {
call print(3)
}
} Else {
call print(1)
}
call print(12)
return 10
}
20 changes: 20 additions & 0 deletions XSharp/test/Parser/isprime.xsharp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
boolean isPrime(i32 x){
if(x <= 1)
return false;
i32 i = 2;
while(i*i <= x){
if(x%i == 0){return false;}
i=i+1;
}
return true;
}

i32 main()
{
boolean a = true;
while(a){
i32 x = inputI32();
print(isPrime(x));
}
return 32;
}
19 changes: 19 additions & 0 deletions XSharp/test/Parser/isprime.xsharp_ast.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
func [isPrime](Var x:i32)->[boolean] {
If([x] <= [1])return false
Var i:i32 = 2
while([[i] * [i]] <= [x]){
If([[x] % [i]] == [0]){
return false
}
[i] = [[i] + [1]]
}
return true
}
func [main]()->[i32] {
Var a:boolean = true
while(a){
Var x:i32 = call inputI32()
call print(call isPrime(x))
}
return 32
}
12 changes: 12 additions & 0 deletions XSharp/test/Parser/recursive.xsharp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
i32 fib(i32 n){
if(n <= 0)
return -3;
if(n==1 || n==2)
return 1;
return fib(n-1) + fib(n-2);
}

i32 main() {
print(fib(inputI32()));
return 3;
}
9 changes: 9 additions & 0 deletions XSharp/test/Parser/recursive.xsharp_ast.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
func [fib](Var n:i32)->[i32] {
If([n] <= [0])return -[3]
If([[n] == [1]] || [[n] == [2]])return 1
return [call fib([n] - [1])] + [call fib([n] - [2])]
}
func [main]()->[i32] {
call print(call fib(call inputI32()))
return 3
}
30 changes: 30 additions & 0 deletions XSharp/test/Parser/string.xsharp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class string extends A{
char[] data;

new(char[] chars) {
self.data = new char[](chars.length);
i64 i = 0;
while(i < chars.length) {
self.data[i] = chars[i];
i = i + 1;
}
}

char charAt(i64 index) {
return self.data[index];
}

char[] getData(){
print(self.data.length);
return self.data;
}
}

i32 main(){
string a = new string("H,world!\n");
print("H,world!\n"[1]);
print(a.getData());
a.getData()[3] = '$';
print(a.charAt(3));
return 0;
}
28 changes: 28 additions & 0 deletions XSharp/test/Parser/string.xsharp_ast.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Class string extends A{
field : constructor(Var chars:Array<char, 1>){{
[self.data] = [call Array<char, 1>(chars.length)]
Var i:i64 = 0
while([i] < [chars.length]){
[i in self.data] = [i in chars]
[i] = [[i] + [1]]
}
}}
field : Var data:Array<char, 1>
field : string::func [charAt](Var index:i64)->[char] {
return index in self.data
}
field : string::func [getData]()->[Array<char, 1>] {
call print(self.data.length)
return self.data
}
}
func [main]()->[i32] {
Var a:string = call string("H,world!
")
call print(1 in "H,world!
")
call print(call a.getData())
[3 in call a.getData()] = ['$']
call print(call a.charAt(3))
return 0
}
12 changes: 12 additions & 0 deletions XSharp/test/Parser/types.xsharp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
i64 add(i32 a,i32 b){
return a+b;
}

i32 main()
{
print(2);
print(2+1.1);
print(2/1.1);
print((2==1.1)||true);
return 21;
}
10 changes: 10 additions & 0 deletions XSharp/test/Parser/types.xsharp_ast.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
func [add](Var a:i32,Var b:i32)->[i64] {
return [a] + [b]
}
func [main]()->[i32] {
call print(2)
call print([2] + [1.100000])
call print([2] / [1.100000])
call print([[2] == [1.100000]] || [true])
return 21
}
29 changes: 21 additions & 8 deletions XSharpCLI/astprint.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#include "XSharp/Lexer.h"
#include "XSharp/Parser.h"
#include <unistd.h>
#include "CLI11.hpp"
#include "fmt/core.h"
#include <fcntl.h>
#include <fstream>

XString inputFile;
XString OutputFilename;

int main(int argc, char* argv[])
{
if (argc <= 1) {
printf(
"Arguments and options missing, please enter '--help' to get "
"help\n");
return 1;
}
CLI::App app("The compiler of XSharp");
app.add_option("-o", OutputFilename, "Where to put the executable");
app.add_option("Input files", inputFile, "Input files to compile")
->required();
CLI11_PARSE(app, argc, argv);

char* path = argv[1];
char buffer[10028];
Expand All @@ -22,10 +27,18 @@ int main(int argc, char* argv[])
try {
XSharp::Lexer lexer;
XSharp::Parser parser;

auto ast = parser.parse(lexer.tokenize(buffer));
fmt::print("{}", ast->dump());
delete ast;

if (OutputFilename.size() == 0) {
fmt::print("{}", ast->dump());
} else {
std::ofstream output(OutputFilename.toStdString());
output << ast->dump().toStdString();
output.close();
}

delete ast;
} catch (XSharpError& e) {
fmt::print("ERROR:{}\n", e.errorInfo);
}
Expand Down
Loading

0 comments on commit 9631f62

Please sign in to comment.