-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountup.rs
78 lines (77 loc) · 2.98 KB
/
countup.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use sila_transpiler_infrastructure::{
transpile_javascript, transpile_python, transpile_ruby, Block, Expr, Instruction, Library,
Operator, Type,
};
fn main() {
let program: Block = vec![
Instruction::Function(
"show".to_string(),
vec!["count".to_string()],
vec![Instruction::Return(Some(Expr::Expr(vec![
Expr::Literal(Type::String("counter value is ".to_string())),
Expr::Operator(Operator::Add),
Expr::Library(Library::ToString, vec![Expr::Variable("count".to_string())]),
])))],
),
Instruction::Let("count".to_string(), Expr::Literal(Type::Integer(0))),
Instruction::Let("limit".to_string(), Expr::Literal(Type::None)),
Instruction::TryError(
vec![Instruction::Variable(
"limit".to_string(),
Expr::Library(
Library::ToInterger,
vec![Expr::Library(
Library::Input,
vec![Expr::Literal(Type::String("limit: ".to_string()))],
)],
),
)],
vec![Instruction::Variable(
"limit".to_string(),
Expr::Library(
Library::Round,
vec![Expr::Expr(vec![
Expr::Library(Library::Random, vec![]),
Expr::Operator(Operator::Mul),
Expr::Literal(Type::Integer(100)),
])],
),
)],
),
Instruction::While(
Expr::Expr(vec![
Expr::Variable("count".to_string()),
Expr::Operator(Operator::Less),
Expr::Variable("limit".to_string()),
]),
vec![
Instruction::Variable(
"count".to_string(),
Expr::Expr(vec![
Expr::Variable("count".to_string()),
Expr::Operator(Operator::Add),
Expr::Literal(Type::Integer(1)),
]),
),
Instruction::If(
Expr::Expr(vec![
Expr::Variable("count".to_string()),
Expr::Operator(Operator::Mod),
Expr::Literal(Type::Integer(2)),
Expr::Operator(Operator::Equal),
Expr::Literal(Type::Integer(0)),
]),
vec![Instruction::Continue],
None,
),
Instruction::Print(Expr::Call(
"show".to_string(),
vec![Expr::Variable("count".to_string())],
)),
],
),
];
println!("JavaScript:\n{}\n", transpile_javascript(program.clone()));
println!("Ruby:\n{}\n", transpile_ruby(program.clone()));
println!("Python:\n{}\n", transpile_python(program));
}