readme version 2
This commit is contained in:
98
README.md
98
README.md
@@ -28,12 +28,52 @@ You can also declare a constant using the `const` keyword instead.
|
||||
```
|
||||
const x int = 9;
|
||||
```
|
||||
---
|
||||
|
||||
### Scopes
|
||||
|
||||
Like in most languages, variables in One are only limited to their scope.
|
||||
You can access a variable from a deeper nested scope, but not the opposite:
|
||||
|
||||
```
|
||||
let x int = 5;
|
||||
if 5 > 0 {
|
||||
x = 2;
|
||||
}
|
||||
```
|
||||
|
||||
is valid code, while
|
||||
|
||||
```
|
||||
if 3 < 10 {
|
||||
let y int = 288;
|
||||
}
|
||||
y = 10;
|
||||
```
|
||||
|
||||
isn't valid, as the variable `y` cannot be access outside its scope.
|
||||
|
||||
You can also create scopes by wrapping statements in braces. This lets you declare
|
||||
variables with the same name in different scopes, for example:
|
||||
|
||||
```
|
||||
let x int = 3
|
||||
{
|
||||
let y real = 5.5;
|
||||
print(y);
|
||||
}
|
||||
print("y is now undefined again here");
|
||||
{
|
||||
let y real = 0.2;
|
||||
print(y);
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Primitive Types
|
||||
|
||||
As of now, One has a total of 4 primitive types: `int`, `real`, `bool` and `string`.
|
||||
As of now, One has a total of 4 primitive types: `void`. `int`, `real`, `bool` and `string`.
|
||||
There are no methods for types, but you can cast a variable to one of them with this syntax
|
||||
|
||||
```
|
||||
@@ -44,13 +84,14 @@ 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.
|
||||
Variables cannot be declared with type `void`.
|
||||
|
||||
---
|
||||
|
||||
### 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
|
||||
You declare a function called `foo` which takes an `int` and returns a `bool` with the following syntax:
|
||||
|
||||
```
|
||||
fn foo(myarg int) bool {
|
||||
@@ -58,7 +99,9 @@ fn foo(myarg int) bool {
|
||||
}
|
||||
```
|
||||
|
||||
Calling a function looks like this
|
||||
If a function is not expected to return anything, the return type must be `void`.
|
||||
|
||||
Calling a function looks like this:
|
||||
|
||||
```
|
||||
let b bool = foo(9);
|
||||
@@ -69,8 +112,8 @@ 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
|
||||
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 {
|
||||
@@ -80,13 +123,16 @@ class User {
|
||||
}
|
||||
```
|
||||
|
||||
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
|
||||
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}
|
||||
```
|
||||
|
||||
Class types can be used anywhere primitive types can, such as function arguments
|
||||
or other classes' members.
|
||||
|
||||
---
|
||||
|
||||
### Print
|
||||
@@ -100,14 +146,14 @@ let x int = 144;
|
||||
print(x, 240);
|
||||
```
|
||||
|
||||
produces this output
|
||||
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
|
||||
class-type variables will be printed with special formatting which, if we takes
|
||||
the User class and variable we defined earlier, will look like this.
|
||||
|
||||
```
|
||||
User {
|
||||
@@ -117,4 +163,36 @@ User {
|
||||
}
|
||||
```
|
||||
|
||||
### Control Flow
|
||||
|
||||
Control flow in One is still quite limited, as the `for` keyword has not been implemented yet. `if`, `else` and `elif` statements are fully implemented and are written like this.
|
||||
|
||||
```
|
||||
let x int = 17;
|
||||
|
||||
if x >= 100 {
|
||||
print(x, "is greater than 100");
|
||||
} elif x >= 10 {
|
||||
print(x, "is greater than 10"):
|
||||
} else {
|
||||
print(x, "is less than 10");
|
||||
}
|
||||
```
|
||||
|
||||
Using parentheses around the condition isn't necessary.
|
||||
|
||||
### The main function
|
||||
|
||||
It is important that every One program has an entry point called `main`.
|
||||
The 'main' function must always return `int` and does not accept any arguments.
|
||||
|
||||
```
|
||||
fn main() int {
|
||||
print("my code here");
|
||||
}
|
||||
```
|
||||
|
||||
It isn't necessary to explicitly insert a `return` statement in the main function.
|
||||
Returning from the main function will exit the program with an exit code equal
|
||||
to the returned integer.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user