Skip to content

Commit e123c40

Browse files
authored
Merge pull request #14 from Delta456/master
Primitives, Variables, Operators and Mutability
2 parents 8d5a112 + ccc42d0 commit e123c40

8 files changed

Lines changed: 229 additions & 31 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*.vrepl_temp.v
55
*.tmp.c
66
a.out.tmp.c
7+
*.vrepl.v
8+
*.vrepl
79

810
.DS_Store
911
data/.DS_Store

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# V by Example
22

3-
> Learn V by Example
3+
> Learn V by Examples
44
55
Note: This is still a work in progress
66

7+
Discord server : https://discord.gg/g33GaP
8+
79
## Contributing
810

911
For contributing see [CONTRIBUTING.md](CONTRIBUTING.md)

src/hello_world/README.md

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,14 @@
55
*
66
*/
77

8-
struct foo {
9-
name string
10-
number int
11-
}
12-
13-
fn main() {
14-
158
print('Hello V\n')
9+
print('Hello World')
1610

1711
// This will print on a new line automatically
18-
println('Hello V by Example')
12+
// println('Hello V by Example')
13+
// eprintln('Hello V by Example')
1914

2015
/*println('I am Bob and I am $age old') */ // This won't print because `age` isn't defined.
2116
/* println(foo) */ // Also wouldn't work because custom str func not defined .
2217

23-
panic('Exiting from the program')
24-
25-
}
18+
//panic('Exiting from the program')

src/hello_world/hello.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Hello V
2+
3+
## Formatted Print
4+
5+
Printing is handled by various I/O stream functions. One should know where to use them accordingly.
6+
7+
- `print`: for printing the text to the output stream without a newline.
8+
9+
- `println`: same as `print` but newline appended automatically.
10+
11+
- `eprint`: same as `print` but the output goes to error stream (stderr).
12+
13+
- `eprintln`: same as `println` but the output goes to error stream (stderr).
14+
15+
- `panic`: outputs and exits from the program.
16+
17+
```go
18+
print('Hello World')
19+
print('Hello V')
20+
```
21+
22+
This will print `Hello WorldHello V`
23+
24+
If you want to print the next line on a new line you would have to do `\n`.
25+
```go
26+
print('Hello World \n')
27+
print('Hello V ')
28+
```
29+
If you don't want to use `\n` then you can use `println` instead.
30+
31+
## Comments
32+
33+
V supports single line comments `//` and mutli-line comments `/* */`. They should be used for documenting the code for letting the other users know how the code works. It can also be used for temporarily commenting the code which has to be used later on.
34+
35+
```go
36+
// This is a single line comment
37+
38+
/* This is a
39+
* multi-line comment
40+
* /* This could be nested as well*/
41+
*/
42+
```
43+
44+
## Exercises
45+
46+
Try uncommenting the code in `hello.v` and see what happens.

src/keywords.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Keywords
2+
3+
V is a very small language so it has less keywords. There are around 25 keywords.
4+
5+
| break | const | continue | return | defer |
6+
|-------|--------|----------|---------|-----------|
7+
| else | enum | fn | for | struct |
8+
| go | goto | if | else | else if |
9+
| type | pub | fn | in | interface |
10+
| match | module | mut | none | or |
11+
12+
Like other languages, they cannot be used as a variable.

src/operator.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
## Operators
2+
3+
V supports the following operators:
4+
5+
### Basic Operators
6+
7+
- `+` (addition) for int, float and string
8+
9+
- `-` (subtraction) for int and float
10+
11+
- `*` (multiplication) for int and float
12+
13+
- `/` (division) for int and float
14+
15+
- `%` (modulos) for int
16+
17+
```go
18+
println(3 + 5)
19+
println(2.0 + 5.0)
20+
println('hello' + 'world')
21+
22+
println(9 - 10)
23+
println(7.0 - 5.0)
24+
25+
println(3 * 5)
26+
println(2.0 * 4)
27+
28+
println(23 / 3)
29+
println(25.0 / 5.0)
30+
31+
println(27 % 5)
32+
println(27 % 3)
33+
```
34+
Output :
35+
```go
36+
8
37+
7.0
38+
hello world
39+
40+
-1
41+
2.0
42+
43+
15
44+
8.0
45+
46+
7
47+
5.0
48+
49+
2
50+
0
51+
```
52+
53+
**NOTE**: Unlike other languages, V doesn't allow modulus with float.
54+
55+
### Comparsion Operators
56+
57+
- `>` greater than
58+
59+
- `<` lesser than
60+
61+
- `==` equal to
62+
63+
- `>=` greater than equal to
64+
65+
- `<=` lesser than equal to
66+
67+
- `!=` not equal to
68+
69+
### Boolean Operators
70+
71+
- `&&` and
72+
73+
- `||` or
74+
75+
- `!` not
76+
77+
78+
### Bitwise Operators
79+
80+
- `>>` left bitshift
81+
82+
- `<<` right bitshift
83+
84+
- `&` bitwise and
85+
86+
- `|` bitwise or
87+
88+
- `^` bitwise xor
89+
90+
### Assignments Operators
91+
92+
- `+=` same as `foo = foo + var`
93+
94+
- `-=` same as `foo = foo - var`
95+
96+
- `*=` same as `foo = foo * var`
97+
98+
- `/=` same as `foo = foo / var`
99+
100+
- `&=` same as `foo = foo & var`
101+
102+
- `|=` same as `foo = foo | var`
103+
104+
- `>>=` same as `foo = foo >> var`
105+
106+
- `<<=` same as `foo = foo << var`
107+
108+
### Special Operators
109+
110+
- `in` for membership
111+
112+
- `none` for optional

src/primitives/primitives.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Primitives
2+
3+
V has less number of primitive types like Go.
4+
5+
### Basic Types
6+
7+
- bool either `true` or `false`
8+
9+
- string
10+
11+
- integer type `int`
12+
13+
- float type `float`
14+
15+
- rune (Unicode string)
16+
17+
18+
## Compound Types
19+
20+
- arrays `[]`
21+
22+
- map `{}`
23+
24+
- struct
25+
26+
## Integer
27+
28+
Integer is sub-classified into `signed` and `unsigned`. `signed` means positive or negative and `unsigned` means positive only.
29+
30+
31+
### Singed Integer
32+
33+
| Type | Size | Range |
34+
|--------|:--------:|----------------------------------------:|
35+
| int8 | 8 bits | -128 to 2<sup>7</sup> -1 |
36+
| int16 | 16 bits | -2<sup>15</sup> to 2<sup>15</sup> - 1 |
37+
| int32 | 32 bits | -2<sup>31</sup> to 2<sup>31</sup> - 1 |
38+
| int64 | 64 bits | -2<sup>63</sup> to 2<sup>63</sup> - 1 |
39+
| int128 | 128 bits | -2<sup>127</sup> to 2<sup>127</sup> - 1 |
40+
41+
### Unsigned Integer
42+
43+
| Type | Size | Range |
44+
|--------|:--------:|----------------------------------------:|
45+
| u8 | 8 bits | 0 to 2<sup>17</sup> -1|
46+
| u16 | 16 bits | 0 to 2<sup>15</sup> - 1 |
47+
| u32 | 32 bits | 0 to 2<sup>31</sup> - 1 |
48+
| u64 | 64 bits | 0 to 2<sup>63</sup> - 1 |
49+
| u128 | 128 bits | 0 to 2<sup>127</sup> - 1 |
50+

0 commit comments

Comments
 (0)