readme first version
This commit is contained in:
120
README.md
Normal file
120
README.md
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
|
||||||
|
# One
|
||||||
|
|
||||||
|
One is a statically typed language inspired by C, V, and go.
|
||||||
|
The compiler is still in development, and may present issues when compiling One code.
|
||||||
|
Note that you need to have eitehr gcc or clang installed to compile One code, as the compiler
|
||||||
|
produces C code that later gets compiled to an executable.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Syntax
|
||||||
|
|
||||||
|
One's syntax rules are meant to be easily understandable and, more importantly, never ambiguous.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Variables
|
||||||
|
|
||||||
|
In One, variables are declared with the `let` keyword.
|
||||||
|
The syntax will look something like this
|
||||||
|
|
||||||
|
```
|
||||||
|
let x int = 6;
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also declare a constant using the `const` keyword instead.
|
||||||
|
|
||||||
|
```
|
||||||
|
const x int = 9;
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Primitive Types
|
||||||
|
|
||||||
|
As of now, One has a total of 4 primitive types: `int`, `real`, `bool` and `string`.
|
||||||
|
There are no methods for types, but you can cast a variable to one of them with this syntax
|
||||||
|
|
||||||
|
```
|
||||||
|
let myreal real = 1.5;
|
||||||
|
let myint int = int(myreal);
|
||||||
|
```
|
||||||
|
|
||||||
|
Right now, strings are implemented as direct counterparts to `char*` in C, which is what they
|
||||||
|
get translated to during compilation. Comparisons and operations on strings are still work in progress
|
||||||
|
and quite buggy, but you are free to use string literals for prints or simple variable declarations with no issues.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Functions
|
||||||
|
|
||||||
|
Functions in One are very similar to what you see in languages such as go.
|
||||||
|
You declare a function called `foo` which takes an `int` and returns a `bool` with the following syntax
|
||||||
|
|
||||||
|
```
|
||||||
|
fn foo(myarg int) bool {
|
||||||
|
return myarg < 10;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Calling a function looks like this
|
||||||
|
|
||||||
|
```
|
||||||
|
let b bool = foo(9);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Classes
|
||||||
|
|
||||||
|
Right now, Classes in One are very similar to C structs. They are declared using the `class` keyword
|
||||||
|
followed by the class name and a block with a list of class members. Defining a class User might look
|
||||||
|
something like this
|
||||||
|
|
||||||
|
```
|
||||||
|
class User {
|
||||||
|
age int
|
||||||
|
name string
|
||||||
|
mail_verified bool
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Creating an instance of a class is very simple, just write the name of the class followed by braces and
|
||||||
|
a list of initial values for the class members
|
||||||
|
|
||||||
|
```
|
||||||
|
let myuser User = User{17, "uan", false}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Print
|
||||||
|
|
||||||
|
Printing is still a work in progress feature, but right now you can print any primitive and non-primitive type.
|
||||||
|
The 'print' built-in function accepts a variable number of arguments, and will print allf
|
||||||
|
of them separated by a space.
|
||||||
|
|
||||||
|
```
|
||||||
|
let x int = 144;
|
||||||
|
print(x, 240);
|
||||||
|
```
|
||||||
|
|
||||||
|
produces this output
|
||||||
|
|
||||||
|
```
|
||||||
|
144 240
|
||||||
|
```
|
||||||
|
|
||||||
|
class-type variables will be printed with special formatting which, if we take the User class and variable
|
||||||
|
we defined earlier, will look like this
|
||||||
|
|
||||||
|
```
|
||||||
|
User {
|
||||||
|
age: 17
|
||||||
|
name: uan
|
||||||
|
mail_verified: false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
@@ -193,7 +193,6 @@ fn (mut g Generator) gen_expr(expr Expr) {
|
|||||||
g.out.write_string('print_${class_name}(')
|
g.out.write_string('print_${class_name}(')
|
||||||
g.gen_expr(inner_expr)
|
g.gen_expr(inner_expr)
|
||||||
g.out.write_string(', 0);')
|
g.out.write_string(', 0);')
|
||||||
} else {
|
|
||||||
g.out.write_string('printf(\"')
|
g.out.write_string('printf(\"')
|
||||||
format := get_type_format(expr_type)
|
format := get_type_format(expr_type)
|
||||||
g.out.write_string('%${format}')
|
g.out.write_string('%${format}')
|
||||||
|
|||||||
Reference in New Issue
Block a user