Merge branch '20-update-readme' into 'master'

Resolve "Update Readme"

Closes #20

See merge request mkais001/klang!13
This commit is contained in:
Marvin Kaiser
2020-02-04 11:46:35 +01:00
2 changed files with 74 additions and 25 deletions

View File

@@ -28,9 +28,6 @@ The makefile can be used to perform various functions more easily:
# Functionality # Functionality
The KLang compiler supports generation of AMD64 assembly code, as well as prettifying and evaluating the KLang code. The KLang compiler supports generation of AMD64 assembly code, as well as prettifying and evaluating the KLang code.
## Data Types
- Integer
## Simple Expressions ## Simple Expressions
The following simple expressions are supported. Expressions need to be put in paranthesis. When using comparison operators, the expressions evaluate to 0 for false and 1 for true. The following simple expressions are supported. Expressions need to be put in paranthesis. When using comparison operators, the expressions evaluate to 0 for false and 1 for true.
- Addition (+) - Addition (+)
@@ -53,16 +50,17 @@ The following simple expressions are supported. Expressions need to be put in pa
``` ```
## Functions ## Functions
Functions can be defined and called. A function call can be used like any other expression. Recusion is supported Functions can be defined and called. A function call can be used like any other expression. Recursion is supported
### Examples ### Examples
``` ```
function fun(x, y, z) { function fun(x: int, y: int, z: bool): int {
return x; return x;
} }
fun(1, 2, 3); fun(1, 2, 3);
``` ```
## Statements ## Statements
Several statements are supported: Several statements are supported:
- if - if
@@ -75,12 +73,12 @@ Several statements are supported:
### Examples ### Examples
``` ```
function example(x, y, z) { function example(x: int, y: int, z: int): int {
let a; let a: int;
let b = 0; let b: int = 0;
if ((x == y)) { if (x == y) {
a = y; a = y;
} else if ((x == z)) { } else if (x == z) {
a = z; a = z;
} else { } else {
return b; return b;
@@ -88,28 +86,79 @@ function example(x, y, z) {
return a; return a;
} }
function whileExample(end) { function whileExample(end: int): int {
let x = 0; let x: int = 0;
while ((x < end)) { while (x < end) {
x = (x + 1); x = x + 1;
} }
return x; return x;
} }
function doWhileExample(end) { function doWhileExample(end: int): int {
let x = 0; let x: int = 0;
do { do {
x = (x + 1); x = x + 1;
} while((x < end)); } while(x < end);
return x;
} }
function forExample(end) { function forExample(end: int): int {
let x = 0; let x: int = 0;
for (let i = 0; (i < end); i = (i + 1)) { for (let i: int = 0; i < end; i = i + 1) {
x = (x + 1); x = x + 1;
} }
return x; return x;
} }
``` ```
## Statically typed
KLang statically verifies the integrity of your code. These checks include:
- Type checking
- Ensuring that variables and functions in use are declared
- Ensuring that the arguments of a function call match the function definition
- Ensuring that a function returns something
- Ensuring that a function only returns data of the correct type
### Data Types
- Integer "int"
- Boolean "bool"
### Examples
You can declare types for parameters, return values and variables
```
function foo(start: int): boolean {
let threshold: int = 10;
return threshold < start;
}
```
Type annotations are required as per our parsing rules, so this will result in an error while parsing
```
function bar() {
return 0;
}
```
This will throw an error since a boolean is returned, but int is declared as the return type
```
function baz(): int {
return false;
}
```
This will throw an error since the function "bam" expects one argument but the call to this function provided none
```
function bam(a: int): int {
return a;
}
bam();
```
This will throw an error since the first parameter of function "boo" has to be of type bool
```
function boo(a: bool): bool {
return a;
}
boo(100);
```

View File

@@ -187,12 +187,11 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
if (ctx.expression() != null) { if (ctx.expression() != null) {
Node expression = this.visit(ctx.expression()); Node expression = this.visit(ctx.expression());
try { try {
declaredType = declaredType.combine(expression.type); declaredType.combine(expression.type);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage()); throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
} }
result = new VariableDeclaration(name, (Expression) expression); result = new VariableDeclaration(name, (Expression) expression);
result.type = declaredType; // add the type only if there is an expression
} else { } else {
result = new VariableDeclaration(name); result = new VariableDeclaration(name);
} }
@@ -202,6 +201,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
result.line = line; result.line = line;
result.col = col; result.col = col;
result.type = declaredType;
return result; return result;
} }