Skip to content

Commit 58588c7

Browse files
committed
version 1.2.0
- added new operators: >= <= - fixed bug with lists not handling strings correctly - fixed bug with arguemnt lists not handling strings correctly - fixed bug with scopes beeing able to access same scope while a recusion - fixed bug in lexer not handling braces correctly when in quotes - fixed list command: replace - fixed string command: replace - fixed math command: max - fixed math command: min - added new os command: utime - added new os command: time - added new example: clock - added new example: brainfuck
1 parent ca222fc commit 58588c7

21 files changed

Lines changed: 493 additions & 131 deletions

examples/brainfuck.mws

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using os
2+
using filesystem
3+
4+
event public error(message :: String)
5+
6+
listen error(message :: String) {
7+
print "An error occured:"
8+
print message
9+
os.exit(1)
10+
}
11+
12+
func get_file()->String {
13+
new arg_list {os.args()}
14+
if({arg_list.length()} == 0) {
15+
occur error("No file given!")
16+
}
17+
new fname {arg_list.at(0)}
18+
if({filesystem.exists fname} == 0) {
19+
occur error("Given file does not exist!")
20+
}
21+
return fname
22+
}
23+
24+
new data [0]
25+
new index 0
26+
27+
func parse(content :: String)->Void {
28+
new c ""
29+
30+
new i 0
31+
while(i < {content.length()}) {
32+
set c {content.at(i)}
33+
if(c == "+") {
34+
data.replace(index,({data.at(index)}+1))
35+
}
36+
elif(c == "-") {
37+
data.replace(index,({data.at(index)}-1))
38+
}
39+
elif(c == "<") {
40+
if(index == 0) {
41+
occur error("Index out of bounce!")
42+
}
43+
set index (index - 1)
44+
}
45+
elif(c == ">") {
46+
while((index+1) >= {data.length()}) {
47+
data.push_back(0)
48+
}
49+
set index (index + 1)
50+
}
51+
elif(c == ".") {
52+
print {data.at(index)}
53+
}
54+
elif(c == "]") {
55+
occur error("Unexcpected token: ]")
56+
}
57+
elif(c == "[") {
58+
new loop_content ""
59+
set i (i+1)
60+
while(i < {content.length()} && {content.at(i)} != "]") {
61+
set loop_content (loop_content + {content.at(i)})
62+
set i (i+1)
63+
}
64+
while({data.at(index)} != 0) {
65+
parse(loop_content)
66+
}
67+
}
68+
else {
69+
# ignore
70+
}
71+
72+
set i (i+1)
73+
}
74+
}
75+
76+
if({os.main_file()}) {
77+
new file {get_file()}
78+
parse({filesystem.read file})
79+
}

examples/clock.mws

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
using os
2+
using math
3+
4+
# distance from a to b
5+
func distance(a :: Number,b :: Number)->Number {
6+
return ({math.max(a,b)} - {math.min(a,b)})
7+
}
8+
9+
# returns b if a is closer to b and c if a is closer to c
10+
func closer_to(a :: Number,b :: Number,c :: Number)->Number {
11+
if({distance(a,b)} < {distance(a,c)}) {
12+
return b
13+
}
14+
return c
15+
}
16+
17+
# returns the closest direction for a given time
18+
func closest(a :: Number)->Number {
19+
if({closer_to(a,45,15)} == 15) {
20+
if({closer_to(a,0,30)} == 30) {
21+
return {closer_to(a,15,30)}
22+
}
23+
else {
24+
return {closer_to(a,0,15)}
25+
}
26+
}
27+
else {
28+
if({closer_to(a,0,30)} == 30) {
29+
return {closer_to(a,45,30)}
30+
}
31+
else {
32+
return {closer_to(a,0,45)}
33+
}
34+
}
35+
}
36+
37+
func get_min_pos()->List {
38+
new L0 [
39+
["","","","","","","","",""],
40+
["","","","","|","","","",""],
41+
["","","","","|","","","",""],
42+
["","","","","","","","",""],
43+
["","","","","","","","",""],
44+
["","","","","","","","",""],
45+
["","","","","","","","",""]
46+
]
47+
new L15 [
48+
["","","","","","","","",""],
49+
["","","","","","","","",""],
50+
["","","","","","","","",""],
51+
["","","","","","-","-","-",""],
52+
["","","","","","","","",""],
53+
["","","","","","","","",""],
54+
["","","","","","","","",""]
55+
]
56+
new L30 [
57+
["","","","","","","","",""],
58+
["","","","","","","","",""],
59+
["","","","","","","","",""],
60+
["","","","","","","","",""],
61+
["","","","","|","","","",""],
62+
["","","","","|","","","",""],
63+
["","","","","","","","",""]
64+
]
65+
new L45 [
66+
["","","","","","","","",""],
67+
["","","","","","","","",""],
68+
["","","","","","","","",""],
69+
["","-","-","-","","","","",""],
70+
["","","","","","","","",""],
71+
["","","","","","","","",""],
72+
["","","","","","","","",""]
73+
]
74+
75+
new clst {closest({os.time("minutes")})}
76+
77+
if(clst == 0) {
78+
return L0
79+
}
80+
elif(clst == 15) {
81+
return L15
82+
}
83+
elif(clst == 30) {
84+
return L30
85+
}
86+
elif(clst == 45) {
87+
return L45
88+
}
89+
}
90+
91+
func get_hour_pos()->List {
92+
new L0 [
93+
["","","","","","","","",""],
94+
["","","","","","","","",""],
95+
["","","","","#","","","",""],
96+
["","","","","","","","",""],
97+
["","","","","","","","",""],
98+
["","","","","","","","",""],
99+
["","","","","","","","",""]
100+
]
101+
new L15 [
102+
["","","","","","","","",""],
103+
["","","","","","","","",""],
104+
["","","","","","","","",""],
105+
["","","","","","#","#","",""],
106+
["","","","","","","","",""],
107+
["","","","","","","","",""],
108+
["","","","","","","","",""]
109+
]
110+
new L30 [
111+
["","","","","","","","",""],
112+
["","","","","","","","",""],
113+
["","","","","","","","",""],
114+
["","","","","","","","",""],
115+
["","","","","#","","","",""],
116+
["","","","","","","","",""],
117+
["","","","","","","","",""]
118+
]
119+
new L45 [
120+
["","","","","","","","",""],
121+
["","","","","","","","",""],
122+
["","","","","","","","",""],
123+
["","","#","#","","","","",""],
124+
["","","","","","","","",""],
125+
["","","","","","","","",""],
126+
["","","","","","","","",""]
127+
]
128+
129+
new clst {closest({os.time("hours")})}
130+
131+
if(clst == 0) {
132+
return L0
133+
}
134+
elif(clst == 15) {
135+
return L15
136+
}
137+
elif(clst == 30) {
138+
return L30
139+
}
140+
elif(clst == 45) {
141+
return L45
142+
}
143+
}
144+
145+
func generate_buffer()->String {
146+
new buffer ">> CLOCK: <<\n"
147+
148+
new min_pos {get_min_pos()}
149+
new hour_pos {get_hour_pos()}
150+
151+
new time_numbers [
152+
["","","","","0","","","",""],
153+
["","","","","","","","",""],
154+
["","","","","","","","",""],
155+
["9","","","","@","","","","3"],
156+
["","","","","","","","",""],
157+
["","","","","","","","",""],
158+
["","","","","6","","","",""]
159+
]
160+
161+
for y in {min_pos.length()} {
162+
new line_m {min_pos.at(y)}
163+
new line_h {hour_pos.at(y)}
164+
new line_t {time_numbers.at(y)}
165+
for x in {line_h.length()} {
166+
new add " "
167+
if({line_m.at(x)} != "") {
168+
set add {line_m.at(x)}
169+
}
170+
if({line_h.at(x)} != "") {
171+
set add {line_h.at(x)}
172+
}
173+
if({line_t.at(x)} != "") {
174+
set add {line_t.at(x)}
175+
}
176+
177+
set buffer (buffer + add)
178+
}
179+
if(y+1 != {min_pos.length()}) {
180+
set buffer (buffer + "\n")
181+
}
182+
}
183+
return buffer
184+
}
185+
186+
func schedule()->Void {
187+
while(1) {
188+
os.clear()
189+
print {generate_buffer()}
190+
os.sleep(3000)
191+
}
192+
}
193+
194+
if({os.main_file()}) {
195+
schedule()
196+
}

examples/tasks/fib.mws

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,25 @@ func fib(a :: Number)->Number {
99
}
1010
}
1111

12+
func fib2(n :: Number)->Number {
13+
new a 1
14+
new b 0
1215

16+
for i in n {
17+
set b (a+b)
18+
set a (b-a)
19+
}
20+
return b
21+
}
22+
23+
# slower
1324
fib(0)
1425
fib(1)
1526
fib(6)
16-
fib(20)
27+
fib(20)
28+
29+
# faster
30+
fib2(0)
31+
fib2(1)
32+
fib2(6)
33+
fib2(20)

inc/defs.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef MEOWSCRIPT_IG_DEFS_HPP
22
#define MEOWSCRIPT_IG_DEFS_HPP
33

4-
#define MEOWSCRIPT_VERSION_PATCH 1
5-
#define MEOWSCRIPT_VERSION_MINOR 1
4+
#define MEOWSCRIPT_VERSION_PATCH 0
5+
#define MEOWSCRIPT_VERSION_MINOR 2
66
#define MEOWSCRIPT_VERSION_MAJOR 1
77

88
#define MEOWSCRIPT_VERSION_STR std::to_string(MEOWSCRIPT_VERSION_MAJOR) + "." + std::to_string(MEOWSCRIPT_VERSION_MINOR) + "." + std::to_string(MEOWSCRIPT_VERSION_MINOR)

inc/expressions.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,22 @@ inline std::unordered_map<std::string,std::vector<Operator>> operators = {
136136
}
137137
},
138138
}},
139+
{"<=", {
140+
{
141+
Variable::Type::Number, Variable::Type::Number, 0,
142+
[](Variable left, Variable right)->Variable {
143+
return left.storage.number <= right.storage.number;
144+
}
145+
},
146+
}},
147+
{">=", {
148+
{
149+
Variable::Type::Number, Variable::Type::Number, 0,
150+
[](Variable left, Variable right)->Variable {
151+
return left.storage.number >= right.storage.number;
152+
}
153+
},
154+
}},
139155
{"&&", {
140156
{
141157
Variable::Type::Number, Variable::Type::Number, -1,

inc/functions.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Function {
1717
std::vector<std::string> arg_names;
1818
unsigned int scope_idx = 0;
1919
Variable::Type return_type = Variable::Type::UNKNOWN;
20-
std::filesystem::path file;
20+
fs::path file;
2121
// Takes care of the required amount of arguments and their types as well as the return value
2222
// Throws `MWSMessageException` on error
2323
Variable run(std::vector<Variable> args);

inc/global.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ namespace global {
3434
bool pop_trace();
3535

3636
inline std::string origin_file = "";
37+
38+
fs::path include_parent_path();
3739
}
3840

3941
MEOWSCRIPT_HEADER_END

0 commit comments

Comments
 (0)