Set is an imperative programming language with focus on teaching mathematics, therefore, it is aimed at teachers and students. The language handles sets as one of the primitive types.
The interpreter of the set programming language was a homework of the Concepts and Paradigms Programming Language course of the Federal University of Rio Grande do Norte (UFRN). The interpreter was developed using the Haskell language. We used Alex to generate the Haskell lexical analyzer for our language and we used the Parsec library to implement the parser and the interpreter.
Note: One of the language's basic primitive type was not implemented due to time issues.
To compile the interpreter use the make.sh
file.
./make
To run a set program use the following command:
./set <file-name>
- Primitive types
- Array type
- If statement
- While statement
- Break statement
- Continue statement
- User input
- Set type
- Matrix type
- Pointer type
- User types
- Function
- Procedure
A set program has the following structure:
program <program-name>
<statements>
end <program-name>
Hello World in set:
program helloworld
print("Hello World!");
end helloworld
program helloworld
# Comments
print("Hello World!");
end helloworld
Nat : x := 1;
Int : x := 1;
Int : y := -1;
Real : x := 1.0;
Real : y := -1.0;
Bool : x := true;
Bool : y := false;
Text : x := "Hello world";
# Array[Type,Size] : x;
Array[Nat,3] : x;
x[0] := 1;
x[1] := 2;
x[2] := 3;
print("{x[0]=" + x[0] + ",x[1]=" + x[1] + ",x[2]=" + x[2] + "}");
Types | Coercion |
---|---|
Nat | Int, Real, Text |
Int | Real, Text |
Real | Text |
Bool | Text |
Text | - |
Array | Text |
-
Sum:
program sum Nat : x := 1; Int : y; y := 1; Int sum := x + y; # Result: 2 print(sum); end sum
-
Subtraction:
program subtraction Nat : x := 1; Int : y := 1; print(x - y); # Result: 0 end subtraction
-
Multiplication:
program multiplication Real : x := 5.0; Int : y := 2; print(x * y); # Result: 10.0 end multiplication
-
Division:
program division Real : x := 10.0; Int : y := 2; print(x / y); # Result: 5.0 end division
-
Equality
program equality print(2 == 2); # Result: True print(2 == 1); # Result: False end equality
-
Conjunction
program conjunction print((1 == 1) && (2 == 2)); # Result: True print((1 == 1) && (2 == 1)); # Result: False print((2 == 1) && (1 == 1)); # Result: False print((2 == 1) && (2 == 1)); # Result: False end conjunction
-
Disjunction
program disjunction print((1 == 1) || (2 == 2)); # Result: True print((1 == 1) || (2 == 1)); # Result: True print((2 == 1) || (1 == 1)); # Result: True print((2 == 1) || (2 == 1)); # Result: False end disjunction
-
Greater than
program greaterthan print(2 > 1); # Result: True print(1 > 2); # Result: False print(1 > 1); # Result: False end greaterthan
-
Smaller than
program smallerthan print(2 < 1); # Result: False print(1 < 2); # Result: True print(1 < 1); # Result: False end smallerthan
-
Greater than or equal to
program greaterthanorequal print(2 >= 1); # Result: True print(1 >= 2); # Result: False print(1 >= 1); # Result: True end greaterthanorequal
-
Smaller than or equal to
program smallerthanorequal print(2 <= 1); # Result: False print(1 <= 2); # Result: True print(1 <= 1); # Result: True end smallerthanorequal
-
Concatenation:
program concat Real : x := 2.0; Real : y := 2.0; Text : s := "of the sum:"; print("Result " + s + (x + y)); # Result: "Result of the sum: 4.0" end concat
The only condition structure is the if-elseif-else
:
program conditionstructure
if (1 > 2)
print("Something wrong.");
elseif (2 > 1)
print("OK.");
else
print("Something wrong.");
endif
end conditionstructure
The only repetition structure is the while
:
program repetitionstructure
Nat : count := 0;
while(count < 10)
count := count + 1;
print(count);
endwhile
end repetitionstructure
program inputoutput
Text : t;
input(t); # Get user input
print(t); # Print user input
program inputoutput
Breno Viana | Felipe Barbalho | Patrícia Cruz | Raul Silveira | Jackson Rauup |