Skip to content

Commit 1f21571

Browse files
committed
support type convertor
1 parent 26ffd37 commit 1f21571

File tree

7 files changed

+185
-42
lines changed

7 files changed

+185
-42
lines changed

csbuild/ecs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"Name": "ecs",
44
"Info": "Extended CovScript(ECS Lang) Header",
55
"Author": "Michael Lee",
6-
"Version": "1.2.4",
6+
"Version": "1.2.5",
77
"Target": "imports/ecs.csp",
88
"Dependencies": [
99
"sdk_extension"

csbuild/ecs_generator.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"Name": "ecs_generator",
44
"Info": "Extended CovScript(ECS Lang) Generator",
55
"Author": "Michael Lee",
6-
"Version": "4.0.0a-230404",
6+
"Version": "4.0.0a-230405",
77
"Target": "imports/ecs_generator.csp",
88
"Dependencies": [
99
"parsergen",

csbuild/ecs_parser.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"Name": "ecs_parser",
44
"Info": "Extended CovScript(ECS Lang) Parser",
55
"Author": "Michael Lee",
6-
"Version": "1.2.6",
6+
"Version": "1.2.7",
77
"Target": "imports/ecs_parser.csp",
88
"Dependencies": [
99
"parsergen",

imports/ecs.csp

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Extended Covariant Script Header: v1.2.4
1+
# Extended Covariant Script Header: v1.2.5
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -164,4 +164,73 @@ function ecs_slice_ext(arr, beg, end, step)
164164
end
165165
end
166166
return move(slice_data)
167-
end
167+
end
168+
169+
class invalid_type
170+
var error_text = new string
171+
function construct(msg)
172+
error_text = to_string(msg)
173+
end
174+
function what()
175+
return error_text
176+
end
177+
end
178+
179+
namespace type_constructor
180+
function __number(val)
181+
switch typeid val
182+
default
183+
throw_exception(param_new(invalid_type, {"Invalid type when construct number, require number, char or string."}))
184+
end
185+
case typeid number
186+
return clone(val)
187+
end
188+
case typeid char
189+
return to_integer(val)
190+
end
191+
case typeid string
192+
return val.to_number()
193+
end
194+
end
195+
end
196+
function __integer(val)
197+
return type_constructor.__number(val).ntoi()
198+
end
199+
function __float(val)
200+
return type_constructor.__number(val).ntof()
201+
end
202+
function __char(val)
203+
if typeid val != typeid number || val.is_float()
204+
throw_exception(param_new(invalid_type, {"Invalid type when construct char, require integer."}))
205+
end
206+
return char.from_ascii(val)
207+
end
208+
function __string(val)
209+
return to_string(val)
210+
end
211+
function __list(val)
212+
if typeid val != typeid array
213+
throw_exception(param_new(invalid_type, {"Invalid type when construct list, require array."}))
214+
end
215+
return val.to_list()
216+
end
217+
function __hash_map(val)
218+
if typeid val != typeid array
219+
throw_exception(param_new(invalid_type, {"Invalid type when construct hash map, require array."}))
220+
end
221+
return val.to_hash_map()
222+
end
223+
function __hash_set(val)
224+
if typeid val != typeid array
225+
throw_exception(param_new(invalid_type, {"Invalid type when construct hash set, require array."}))
226+
end
227+
return val.to_hash_set()
228+
end
229+
end
230+
231+
@begin
232+
var internal_type = {
233+
"number", "integer", "float", "char", "string",
234+
"list", "hash_map", "hash_set"
235+
}.to_hash_set()
236+
@end

imports/ecs_generator.csp

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ package ecs_generator
4040
# Master
4141

4242
namespace ecs_info
43-
constant version = "4.0.0 Cuon alpinus(Preview) Build 230404"
44-
constant std_version = "210604"
43+
constant version = "4.0.0 Cuon alpinus(Preview) Build 230405"
44+
constant std_version = "210605"
4545
end
4646

4747
import parsergen, ecs
@@ -130,12 +130,15 @@ class generator
130130
nodes := nodes[0].nodes
131131
if nodes.size == 1 && typeid nodes[0] == typeid parsergen.syntax_tree && nodes[0].root == "mul-expr"
132132
nodes := nodes[0].nodes
133-
if nodes.size == 1 && typeid nodes[0] == typeid parsergen.syntax_tree && nodes[0].root == "unary-expr"
133+
if nodes.size == 1 && typeid nodes[0] == typeid parsergen.syntax_tree && nodes[0].root == "conv-expr"
134134
nodes := nodes[0].nodes
135-
if nodes.size == 1 && typeid nodes[0] == typeid parsergen.syntax_tree && nodes[0].root == "prim-expr"
135+
if nodes.size == 1 && typeid nodes[0] == typeid parsergen.syntax_tree && nodes[0].root == "unary-expr"
136136
nodes := nodes[0].nodes
137-
if nodes.size == 1 && typeid nodes[0] == typeid parsergen.syntax_tree && nodes[0].root == "visit-expr"
138-
return get_id_of_visit_expr(nodes[0].nodes)
137+
if nodes.size == 1 && typeid nodes[0] == typeid parsergen.syntax_tree && nodes[0].root == "prim-expr"
138+
nodes := nodes[0].nodes
139+
if nodes.size == 1 && typeid nodes[0] == typeid parsergen.syntax_tree && nodes[0].root == "visit-expr"
140+
return get_id_of_visit_expr(nodes[0].nodes)
141+
end
139142
end
140143
end
141144
end
@@ -642,35 +645,61 @@ class generator
642645
this.visit_unary_expr(nodes[idx++].nodes)
643646
end
644647
if !matched && (typeid nodes[idx] == typeid parsergen.token_type && nodes[idx].data == "new" || typeid nodes[idx] == typeid parsergen.token_type && nodes[idx].data == "gcnew")
648+
matched = true
645649
# Optional
646650
if nodes.size > 2 && (typeid nodes[2] == typeid parsergen.syntax_tree && nodes[2].root == "array")
647-
matched = true
648-
# Condition
649-
block
650-
var matched = false
651-
if !matched && (typeid nodes[idx] == typeid parsergen.token_type && nodes[idx].data == "new")
652-
matched = true
653-
# Visit term "new"
654-
++idx; target.print(ecs_prefix + "ecs.param_new(")
655-
end
656-
if !matched && (typeid nodes[idx] == typeid parsergen.token_type && nodes[idx].data == "gcnew")
657-
matched = true
658-
# Visit term "gcnew"
659-
++idx; target.print(ecs_prefix + "ecs.param_gcnew(")
651+
var id = get_id_of_visit_expr(nodes[1].nodes)
652+
if id != null && ecs.internal_type.exist(id)
653+
# Condition
654+
block
655+
var matched = false
656+
if !matched && (typeid nodes[idx] == typeid parsergen.token_type && nodes[idx].data == "new")
657+
matched = true
658+
# Visit term "new"
659+
++idx; target.print(ecs_prefix + "ecs.type_constructor.__" + id + "(")
660+
end
661+
if !matched && (typeid nodes[idx] == typeid parsergen.token_type && nodes[idx].data == "gcnew")
662+
matched = true
663+
# Visit term "gcnew"
664+
++idx; target.print(ecs_prefix + "&ecs.type_constructor.__" + id + "(")
665+
end
666+
if !matched
667+
# Error
668+
return
669+
end
660670
end
661-
if !matched
662-
# Error
663-
return
671+
# Recursive Visit visit-expr
672+
idx++
673+
# Recursive Visit array
674+
this.visit_array(nodes[idx++].nodes)
675+
target.print("...)")
676+
else
677+
# Condition
678+
block
679+
var matched = false
680+
if !matched && (typeid nodes[idx] == typeid parsergen.token_type && nodes[idx].data == "new")
681+
matched = true
682+
# Visit term "new"
683+
++idx; target.print(ecs_prefix + "ecs.param_new(")
684+
end
685+
if !matched && (typeid nodes[idx] == typeid parsergen.token_type && nodes[idx].data == "gcnew")
686+
matched = true
687+
# Visit term "gcnew"
688+
++idx; target.print(ecs_prefix + "ecs.param_gcnew(")
689+
end
690+
if !matched
691+
# Error
692+
return
693+
end
664694
end
695+
# Recursive Visit visit-expr
696+
this.visit_visit_expr(nodes[idx++].nodes)
697+
target.print(", ")
698+
# Recursive Visit array
699+
this.visit_array(nodes[idx++].nodes)
700+
target.print(")")
665701
end
666-
# Recursive Visit unary-expr
667-
this.visit_unary_expr(nodes[idx++].nodes)
668-
target.print(", ")
669-
# Recursive Visit array
670-
this.visit_array(nodes[idx++].nodes)
671-
target.print(")")
672702
else
673-
matched = true
674703
# Condition
675704
block
676705
var matched = false
@@ -689,8 +718,8 @@ class generator
689718
return
690719
end
691720
end
692-
# Recursive Visit unary-expr
693-
this.visit_unary_expr(nodes[idx++].nodes)
721+
# Recursive Visit visit-expr
722+
this.visit_visit_expr(nodes[idx++].nodes)
694723
end
695724
end
696725
if !matched && (typeid nodes[idx] == typeid parsergen.syntax_tree && nodes[idx].root == "prim-expr")
@@ -1142,8 +1171,8 @@ class generator
11421171
end
11431172
function visit_mul_expr(nodes)
11441173
var idx = 0
1145-
# Recursive Visit unary-expr
1146-
this.visit_unary_expr(nodes[idx++].nodes)
1174+
# Recursive Visit conv-expr
1175+
this.visit_conv_expr(nodes[idx++].nodes)
11471176
# Optional
11481177
if idx < nodes.size && (typeid nodes[idx] == typeid parsergen.token_type && nodes[idx].data == "*" || typeid nodes[idx] == typeid parsergen.token_type && nodes[idx].data == "/" || typeid nodes[idx] == typeid parsergen.token_type && nodes[idx].data == "%" || typeid nodes[idx] == typeid parsergen.token_type && nodes[idx].data == "^")
11491178
# Condition
@@ -1178,6 +1207,29 @@ class generator
11781207
this.visit_mul_expr(nodes[idx++].nodes)
11791208
end
11801209
end
1210+
function visit_conv_expr(nodes)
1211+
if nodes.size > 1
1212+
# Recursive Visit visit-expr
1213+
link visit_expr_nodes = nodes[2].nodes
1214+
var id = get_id_of_visit_expr(visit_expr_nodes)
1215+
if id != null && ecs.internal_type.exist(id)
1216+
target.print(ecs_prefix + "ecs.type_constructor.__" + id + "(")
1217+
# Recursive Visit unary-expr
1218+
this.visit_unary_expr(nodes[0].nodes)
1219+
target.print(")")
1220+
else
1221+
target.print(ecs_prefix + "ecs.param_new(")
1222+
this.visit_visit_expr(visit_expr_nodes)
1223+
target.print(", {")
1224+
# Recursive Visit unary-expr
1225+
this.visit_unary_expr(nodes[0].nodes)
1226+
target.print("})")
1227+
end
1228+
else
1229+
# Recursive Visit unary-expr
1230+
this.visit_unary_expr(nodes[0].nodes)
1231+
end
1232+
end
11811233
function visit_switch_stmts(nodes)
11821234
var idx = 0
11831235
++indent

imports/ecs_parser.csp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Covariant Script Parser Generator: Grammar of Extended CovScript(ECS Lang) v1.2.6
1+
# Covariant Script Parser Generator: Grammar of Extended CovScript(ECS Lang) v1.2.7
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@ function get_lexical(reg_builder)
3434
"char" : reg_builder("^(\'|\'([^\']|\\\\(0|\\\\|\'|\"|\\w))\'?)$"),
3535
"bsig" : reg_builder("^(;|:=?|::|\\?|\\.\\.?|\\.\\.\\.)$"),
3636
"msig" : reg_builder("^(\\+(\\+|=)?|-(-|=|>)?|\\*=?|/=?|%=?|\\^=?)$"),
37-
"lsig" : reg_builder("^(>|<|&|(\\|)|&&|(\\|\\|)|!|==?|!=?|>=?|<=?)$"),
37+
"lsig" : reg_builder("^(>|<|&|(\\|)|&&|(\\|\\|)|!|=(=|>)?|!=?|>=?|<=?)$"),
3838
"brac" : reg_builder("^(\\(|\\)|\\[|\\]|\\{|\\}|,)$"),
3939
"prep" : reg_builder("^@.*$"),
4040
"ign" : reg_builder("^([ \\f\\r\\t\\v]+|#.*)$"),
@@ -278,11 +278,14 @@ var covscript_syntax = {
278278
syntax.ref("mul-expr"), syntax.optional(syntax.cond_or({syntax.term("+")}, {syntax.term("-")}), syntax.ref("add-expr"))
279279
},
280280
"mul-expr" : {
281-
syntax.ref("unary-expr"), syntax.optional(syntax.nlook(syntax.token("endl")), syntax.cond_or({syntax.term("*")}, {syntax.term("/")}, {syntax.term("%")}, {syntax.term("^")}), syntax.ref("mul-expr"))
281+
syntax.ref("conv-expr"), syntax.optional(syntax.nlook(syntax.token("endl")), syntax.cond_or({syntax.term("*")}, {syntax.term("/")}, {syntax.term("%")}, {syntax.term("^")}), syntax.ref("mul-expr"))
282+
},
283+
"conv-expr" : {
284+
syntax.ref("unary-expr"), syntax.optional(syntax.nlook(syntax.token("endl")), syntax.cond_or({syntax.term("=>")}, {syntax.term("as")}), syntax.ref("visit-expr"))
282285
},
283286
"unary-expr" : {syntax.cond_or(
284287
{syntax.ref("unary-op"), syntax.ref("unary-expr")},
285-
{syntax.cond_or({syntax.term("new")}, {syntax.term("gcnew")}), syntax.ref("unary-expr"), syntax.optional(syntax.ref("array"))},
288+
{syntax.cond_or({syntax.term("new")}, {syntax.term("gcnew")}), syntax.ref("visit-expr"), syntax.optional(syntax.ref("array"))},
286289
{syntax.ref("prim-expr"), syntax.optional(syntax.nlook(syntax.token("endl")), syntax.ref("postfix-expr"))}
287290
)},
288291
"unary-op" : {syntax.cond_or(

tests/v7.ecs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class test
2+
var sum = 0
3+
function construct(...args)
4+
foreach it in args do sum += it
5+
end
6+
function to_string()
7+
system.out.println("Class 'test' to_string called")
8+
return global.to_string(sum)
9+
end
10+
end
11+
system.out.println(3.14 as integer)
12+
system.out.println("3.14" => number)
13+
system.out.println(3.14 as string)
14+
system.out.println({1, 2, 3}... => test)
15+
system.out.println(('!' => integer) as char)
16+
system.out.println({"hello": "world"} as hash_map)
17+
system.out.println({"hello", "world"} => hash_set)
18+
system.out.println(new integer{6.28})
19+
system.out.println(gcnew string{6.28})

0 commit comments

Comments
 (0)