Skip to content

Commit 794652a

Browse files
committed
feat: 完成impl运算符重载
1 parent 82194d0 commit 794652a

6 files changed

Lines changed: 73 additions & 2 deletions

File tree

bin/util.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ let init_env_var_file f =
9393

9494
let init_env_var () =
9595
let home = Sys.getenv "HOME" in
96-
init_env_var_file (home ^ "/.miver/config/miva.toml");
96+
let global_config = home ^ "/.miver/config/miva.toml" in
97+
if Sys.file_exists global_config then
98+
init_env_var_file global_config;
9799
init_env_var_file "miva.toml"
98100

99101

lib/anoncheck.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ let check_anon defs =
8787
| SImportAs (loc, _, _)
8888
| SImportHere (loc, _)
8989
| SExport (loc, _)
90+
| DImpl (loc, _, _)
9091
-> (
9192
match !pre with
9293
| Some DCIntro (_, _) -> (

lib/ast.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,14 @@ and def =
7474
| SImportHere of loc * string
7575
| DCMagical of loc * string
7676
| DCIntro of loc * string
77+
| DImpl of loc * string * impl_expr list
78+
79+
and impl_op =
80+
| ImAdd
81+
| ImSub
82+
| ImMul
83+
| ImEq
84+
| ImNeq
85+
86+
and impl_expr =
87+
| EImpl of loc * impl_op * string

lib/codegen.ml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,35 @@ let cxx_def_of_def indent_level ctx def =
413413
| DTest(_, _, _) -> ""
414414
| DCMagical (_, _) -> ""
415415
| DCIntro (_, _) -> ""
416+
| DImpl (_, s, n) -> (
417+
let ret = ref "" in
418+
List.iter (fun n -> (
419+
let op, fn = match n with
420+
| EImpl (_, op, fn) -> op, fn
421+
in
422+
let ret_typ = match op with
423+
| ImAdd | ImSub | ImMul
424+
-> s
425+
| ImEq | ImNeq
426+
-> "mvp_builtin_boolean"
427+
in
428+
let operator = match op with
429+
| ImAdd -> "+"
430+
| ImSub -> "-"
431+
| ImMul -> "*"
432+
| ImEq -> "=="
433+
| ImNeq -> "!="
434+
in
435+
let typs = s in
436+
let func_sig =
437+
ret_typ ^ " operator" ^ operator ^
438+
"(const " ^ typs ^ "& ____a, const " ^
439+
typs ^ "& ____b) { return " ^ fn ^ "(____a, ____b); }\n"
440+
in
441+
ret := !ret ^ ind ^ func_sig ^ "\n"
442+
)) n;
443+
!ret
444+
)
416445

417446

418447
let cxx_func_declaration func_name params ret_typ_opt =

lib/lexer.mll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ rule token = parse
7878
| "module" { MODULE }
7979
| "export" { EXPORT }
8080
| "import" { IMPORT }
81+
| "impl" { IMPLEMENTS }
82+
83+
| "op_add" { OP_ADD }
84+
| "op_sub" { OP_SUB }
85+
| "op_mul" { OP_MUL }
86+
| "op_eq" { OP_EQ }
87+
| "op_neq" { OP_NEQ }
8188

8289
| "ptr" { PTR }
8390
| "box" { BOX }

lib/parser.mly

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
%token TEST UNSAFE TRUSTED C_KEYWORD
2323
%token EOF
2424
%token PTR BOX ADDR DEREF PTRANY
25-
%token MODULE EXPORT IMPORT
25+
%token OP_ADD OP_SUB OP_MUL OP_EQ OP_NEQ
26+
%token MODULE EXPORT IMPORT IMPLEMENTS
2627

2728
%type <Ast.def> def
2829
%type <Ast.def list> list(def)
@@ -114,6 +115,26 @@ def:
114115
| m = MAGICAL { DCMagical (
115116
{ line = $startpos.Lexing.pos_lnum; col = $startpos.Lexing.pos_cnum - $startpos.Lexing.pos_bol + 1 }
116117
, m) }
118+
| IMPLEMENTS stru = IDENT LBRACE l = separated_nonempty_list(COMMA, impl_decl) RBRACE {
119+
DImpl({
120+
line = $startpos.Lexing.pos_lnum;
121+
col = $startpos.Lexing.pos_cnum - $startpos.Lexing.pos_bol + 1
122+
},
123+
stru, l)
124+
}
125+
126+
impl_decl:
127+
| name = reg_op EQ func = IDENT { EImpl ({
128+
line = $startpos.Lexing.pos_lnum;
129+
col = $startpos.Lexing.pos_cnum - $startpos.Lexing.pos_bol + 1
130+
}, name, func) }
131+
132+
reg_op:
133+
| OP_ADD { ImAdd }
134+
| OP_SUB { ImSub }
135+
| OP_MUL { ImMul }
136+
| OP_EQ { ImEq }
137+
| OP_NEQ { ImNeq }
117138

118139
func_params:
119140
| LPAREN RPAREN { [] }

0 commit comments

Comments
 (0)