Skip to content

Commit 418aa5f

Browse files
committed
Added full compilation of Cythan Stage 1
1 parent 5d00762 commit 418aa5f

File tree

10 files changed

+727
-1
lines changed

10 files changed

+727
-1
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ Cargo.lock
88

99
# These are backup files generated by rustfmt
1010
**/*.rs.bk
11+
12+
13+
# Added by cargo
14+
15+
/target

Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "cythan-compiler"
3+
version = "0.1.0"
4+
authors = ["ccgauche <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
tokesies = {git = "https://github.com/Jeffail/tokesies"}

README.md

+134-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,135 @@
11
# cythan-compiler
2-
A Rust implementation of the Cythan V3 Compiler
2+
3+
A Rust implementation of the Cythan V3 Compiler
4+
5+
## How to program in Cythan Stage 1
6+
7+
### Simplest example
8+
9+
```rust
10+
0 12 2 31 556
11+
```
12+
13+
As you probably know Cythan is a list of numbers, in the case of Cythan Stage 1 numbers are separated by a space.
14+
15+
16+
17+
### Variables
18+
19+
In Cythan stage 1, compile time variables are accepted.
20+
21+
22+
23+
#### To define a variable:
24+
25+
```rust
26+
var1 = (4 10)
27+
```
28+
29+
30+
31+
#### To use a variable:
32+
33+
```rust
34+
0 20 var1 30
35+
```
36+
37+
38+
39+
__Note:__ *Variables in Cythan Stage 1 have no scope and are accessible from everywhere*
40+
41+
42+
43+
### Functions
44+
45+
In Cythan Stage 1 function are computed at compile time like macros.
46+
47+
48+
49+
#### To define a function:
50+
51+
```rust
52+
func1 {
53+
12 20 10
54+
}
55+
```
56+
57+
58+
59+
#### To call a function:
60+
61+
```rust
62+
func1()
63+
```
64+
65+
66+
67+
#### With arguments:
68+
69+
```rust
70+
func1 {
71+
12 20 10 self.0 self.3..10 self.20..40?5
72+
}
73+
74+
func1(10 2 3 45 20 10)
75+
```
76+
77+
78+
79+
As you probably seen `self` keyword is used to represent arguments.
80+
81+
82+
83+
#### Self cheat-sheet:
84+
85+
`self.x..y` returns all numbers from `x` to `y` if one don't exists, it will not be returned
86+
87+
`self.x..y?z` returns all numbers from `x` to `y` if one don't exists, it will be replaced by `z`
88+
89+
`self.x?z` returns the number `x` if it doesn't exists, it will be replaced by `z`
90+
91+
`self.x` returns the number `x` if it doesn't exists, it will not be returned
92+
93+
`self.x..` returns all numbers from `x` to the end of the arguments if one don't exists, it will not be returned
94+
95+
`self.x..?z` returns all numbers from `x` to the end of the arguments if one don't exists, it will be replaced by `z`
96+
97+
98+
99+
## How to compile Cythan Stage 1
100+
101+
From a string:
102+
103+
```rust
104+
use cythan_compiler::*;
105+
106+
tokenizer::Context::new().compute(&tokenizer::generate_tokens(string))
107+
```
108+
109+
110+
111+
From a file:
112+
113+
```rust
114+
use cythan_compiler::*;
115+
116+
tokenizer::Context::new().compute(&tokenizer::generate_tokens(std::fs::read_to_string("file.ct").unwrap()))
117+
```
118+
119+
120+
121+
Get only executable tokens from a file:
122+
123+
```rust
124+
use cythan_compiler::*;
125+
126+
let tokens = tokenizer::generate_tokens(std::fs::read_to_string("file.ct").unwrap();
127+
```
128+
129+
130+
131+
## Run in a cythan machine
132+
133+
134+
135+
To run your generated code into a Cythan machine use [Cythan Rust Library](https://github.com/Cypooos/Cythan-v3)

cythan-in/cythan.ct

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
0 12 2 3
2+
3+
# This is an example
4+
5+
3 # Test
6+
# Test of a multi line comment!
7+
# de
8+
9+
a = (2 10)
10+
11+
b = (4 a)
12+
13+
test_func {
14+
1 3 9 6 8 9 self.0 self.0..12?1 self.1?6 b
15+
}
16+
17+
test_func(23)
18+
19+
4 25 26 23 b

src/lib.rs

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#![feature(test)]
2+
3+
extern crate test;
4+
5+
use test::*;
6+
7+
pub mod tokenizer;
8+
9+
pub use tokenizer::*;
10+
11+
#[test]
12+
fn test() {
13+
let file = r#"0 12 2 3
14+
15+
# This is an example
16+
17+
3 # Test
18+
# Test of a multi line comment!
19+
# de
20+
21+
a = (2 10)
22+
23+
b = (4 a)
24+
25+
test_func {
26+
1 3 9 6 8 9 self.0 self.0..12?1 self.1?6 b
27+
}
28+
29+
test_func(23)
30+
31+
4 25 26 23 b"#;
32+
33+
let tokens = tokenizer::generate_tokens(file);
34+
35+
println!("{:?}",tokens);
36+
37+
println!("{:?}",&tokenizer::Context::new().compute(&tokens));
38+
39+
//assert_eq!(tokens.get(0).unwrap().term(), "hello");
40+
}
41+
42+
#[bench]
43+
fn bench_tokenize(bench: &mut Bencher) {
44+
let file = r#"0 12 2 3
45+
46+
# This is an example
47+
48+
3 # Test
49+
# Test of a multi line comment!
50+
# de
51+
52+
a = (2 10)
53+
54+
b = (4 a)
55+
56+
test_func {
57+
1 3 9 6 8 9 self.0 self.0..12?1 self.1?6 b
58+
}
59+
60+
test_func(23)
61+
62+
4 25 26 23 b"#;
63+
bench.iter(|| {
64+
let a = tokenizer::generate_tokens(file);
65+
})
66+
}
67+
68+
#[bench]
69+
fn bench_compute(bench: &mut Bencher) {
70+
let file = r#"0 12 2 3
71+
72+
# This is an example
73+
74+
3 # Test
75+
# Test of a multi line comment!
76+
# de
77+
78+
a = (2 10)
79+
80+
b = (4 a)
81+
82+
test_func {
83+
1 3 9 6 8 9 self.0 self.0..12?1 self.1?6 b
84+
}
85+
86+
test_func(23)
87+
88+
4 25 26 23 b"#;
89+
let a = tokenizer::generate_tokens(file);
90+
bench.iter(|| {
91+
tokenizer::Context::new().compute(&a)
92+
})
93+
}
94+
95+
#[bench]
96+
fn bench_whole(bench: &mut Bencher) {
97+
let file = r#"0 12 2 3
98+
99+
# This is an example
100+
101+
3 # Test
102+
# Test of a multi line comment!
103+
# de
104+
105+
a = (2 10)
106+
107+
b = (4 a)
108+
109+
test_func {
110+
1 3 9 6 8 9 self.0 self.0..12?1 self.1?6 b
111+
}
112+
113+
test_func(23)
114+
115+
4 25 26 23 b"#;
116+
bench.iter(|| {
117+
tokenizer::Context::new().compute(&tokenizer::generate_tokens(file))
118+
})
119+
}

0 commit comments

Comments
 (0)