-
Notifications
You must be signed in to change notification settings - Fork 0
/
ceylon.tea
155 lines (120 loc) · 3.25 KB
/
ceylon.tea
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// MIT License
//
// Copyright (c) 2021 Ferhat Geçdoğan All Rights Reserved.
// Distributed under the terms of the MIT License.
//
// ceylon - really small & simple interpreter in Gretea.
// ~just for fun~ & ~unusable~
//
// put <expr>
// put <data>
// put <val>
// val <name> = <expr>
// val <name> = <data>
import tea.green.fmt
import tea.green.string
import tea.green.type
import tea.green.char
import tea.green.expr
import tea.green.log
import tea.green.color
import tea.black.sstream
import tea.black.vector
`set nl = "\n"
enum CurrentTok = int8 {
Put,
Val,
Undef
}
compile default for string {
"put \"Hello world\\n\""
+ string("put \"\x1b[0;33mTest\x1b[0m\"\n")
+ string("val x = 9 + 2 - 8\n")
+ string("put $x")
}
fn main() = int {
var mut code: string = [[ default ]]
var mut tok: string = ""
var mut curr = Undef
var mut name: string = ""
var mut data: string = ""
var mut start = false
var mut seq = false
var mut val_list: []string = {}
var mut val_data: []string = {}
var mut is_var_data = false
var mut is_replace_var = false
for &ch in code {
if curr == Val {
if ch == '=' {
is_var_data = true
continue
}
if ch != '=' && !is_var_data {
cpp_str#push_back(name, ch)
}
if ch != '\n' {
cpp_str#push_back(data, ch)
} else {
cpp_vector#push_back(val_list, name)
cpp_vector#push_back(val_data, cpp_str#to_string(expr#eval(data)))
cpp_str#erase(name)
cpp_str#erase(data)
curr = Undef
}
continue
}
if start {
if seq {
if ch == '"' {
cpp_str#push_back(data, ch)
} else if ch == 'n' {
str#append(data, nl)
}
seq = false
continue
}
if ch == '"' && !seq {
start = false
if curr == Put {
cpp_str#erase(tok)
fmt#println(data)
cpp_str#erase(data)
}
continue
} else if is_replace_var {
var mut i = 0
for &tok in val_list {
if tok == tok {
fmt#println(cpp_vector#at(val_data, i))
}
i += 1
}
}
if ch == '\\' {
seq = true
continue
}
cpp_str#push_back(data, ch)
}
if (ch == '"' || ch == '$') && !seq && !start {
if ch == '$' {
is_replace_var = true
}
start = true
continue
}
if ch == '\w' && !str#is_empty(tok) && str#is_empty(data) {
if tok == "put" {
curr = Put
}
else if tok == "val" {
curr = Val
}
cpp_str#erase(tok)
continue
}
if ch == '\n' { continue}
cpp_str#push_back(tok, ch)
}
}