Added assignment of a value to a map binding: m.[i] := v.

This commit is contained in:
Christian Rinderknecht 2019-03-14 13:19:52 +01:00
parent 41f664f197
commit 8e294a013c
No known key found for this signature in database
GPG Key ID: 9446816CFD267040
4 changed files with 87 additions and 41 deletions

35
AST.ml
View File

@ -321,7 +321,7 @@ and instruction =
and single_instr = and single_instr =
Cond of conditional reg Cond of conditional reg
| Match of match_instr reg | Match of match_instr reg
| Ass of ass_instr reg | Ass of ass_instr
| Loop of loop | Loop of loop
| ProcCall of fun_call | ProcCall of fun_call
| Null of kwd_null | Null of kwd_null
@ -358,12 +358,22 @@ and case = {
instr : instruction instr : instruction
} }
and ass_instr = { and ass_instr =
VarAss of var_ass reg
| MapAss of map_ass reg
and var_ass = {
var : variable; var : variable;
ass : ass; ass : ass;
expr : expr expr : expr
} }
and map_ass = {
lookup : map_lookup reg;
ass : ass;
expr : expr
}
and loop = and loop =
While of while_loop reg While of while_loop reg
| For of for_loop | For of for_loop
@ -380,7 +390,7 @@ and for_loop =
and for_int = { and for_int = {
kwd_for : kwd_for; kwd_for : kwd_for;
ass : ass_instr reg; var_ass : var_ass reg;
down : kwd_down option; down : kwd_down option;
kwd_to : kwd_to; kwd_to : kwd_to;
bound : expr; bound : expr;
@ -644,7 +654,8 @@ and record_expr_to_region = function
let instr_to_region = function let instr_to_region = function
Single Cond {region; _} Single Cond {region; _}
| Single Match {region; _} | Single Match {region; _}
| Single Ass {region; _} | Single Ass VarAss {region; _}
| Single Ass MapAss {region; _}
| Single Loop While {region; _} | Single Loop While {region; _}
| Single Loop For ForInt {region; _} | Single Loop For ForInt {region; _}
| Single Loop For ForCollect {region; _} | Single Loop For ForCollect {region; _}
@ -971,12 +982,22 @@ and print_case {value; _} =
print_token arrow "->"; print_token arrow "->";
print_instruction instr print_instruction instr
and print_ass_instr {value; _} = and print_ass_instr = function
VarAss a -> print_var_ass a
| MapAss a -> print_map_ass a
and print_var_ass {value; _} =
let {var; ass; expr} = value in let {var; ass; expr} = value in
print_var var; print_var var;
print_token ass ":="; print_token ass ":=";
print_expr expr print_expr expr
and print_map_ass {value; _} =
let {lookup; ass; expr} = value in
print_map_lookup lookup;
print_token ass ":=";
print_expr expr
and print_loop = function and print_loop = function
While {value; _} -> print_while_loop value While {value; _} -> print_while_loop value
| For for_loop -> print_for_loop for_loop | For for_loop -> print_for_loop for_loop
@ -992,10 +1013,10 @@ and print_for_loop = function
| ForCollect for_collect -> print_for_collect for_collect | ForCollect for_collect -> print_for_collect for_collect
and print_for_int ({value; _} : for_int reg) = and print_for_int ({value; _} : for_int reg) =
let {kwd_for; ass; down; kwd_to; let {kwd_for; var_ass; down; kwd_to;
bound; step; block} = value in bound; step; block} = value in
print_token kwd_for "for"; print_token kwd_for "for";
print_ass_instr ass; print_var_ass var_ass;
print_down down; print_down down;
print_token kwd_to "to"; print_token kwd_to "to";
print_expr bound; print_expr bound;

16
AST.mli
View File

@ -305,7 +305,7 @@ and instruction =
and single_instr = and single_instr =
Cond of conditional reg Cond of conditional reg
| Match of match_instr reg | Match of match_instr reg
| Ass of ass_instr reg | Ass of ass_instr
| Loop of loop | Loop of loop
| ProcCall of fun_call | ProcCall of fun_call
| Null of kwd_null | Null of kwd_null
@ -342,12 +342,22 @@ and case = {
instr : instruction instr : instruction
} }
and ass_instr = { and ass_instr =
VarAss of var_ass reg
| MapAss of map_ass reg
and var_ass = {
var : variable; var : variable;
ass : ass; ass : ass;
expr : expr expr : expr
} }
and map_ass = {
lookup : map_lookup reg;
ass : ass;
expr : expr
}
and loop = and loop =
While of while_loop reg While of while_loop reg
| For of for_loop | For of for_loop
@ -364,7 +374,7 @@ and for_loop =
and for_int = { and for_int = {
kwd_for : kwd_for; kwd_for : kwd_for;
ass : ass_instr reg; var_ass : var_ass reg;
down : kwd_down option; down : kwd_down option;
kwd_to : kwd_to; kwd_to : kwd_to;
bound : expr; bound : expr;

View File

@ -347,7 +347,8 @@ after_instr:
instr_or_end: instr_or_end:
End { End {
`End $1 } `End $1
}
| instruction after_instr { | instruction after_instr {
let instrs, term, close = $2 in let instrs, term, close = $2 in
`Some ($1, instrs, term, close) `Some ($1, instrs, term, close)
@ -456,6 +457,16 @@ case:
} }
ass: ass:
var_ass {
VarAss $1
}
| map_selection ASS expr {
let region = cover $1.region (expr_to_region $3)
and value = {lookup = $1; ass = $2; expr = $3}
in MapAss {region; value}
}
var_ass:
var ASS expr { var ASS expr {
let region = cover $1.region (expr_to_region $3) let region = cover $1.region (expr_to_region $3)
and value = {var = $1; ass = $2; expr = $3} and value = {var = $1; ass = $2; expr = $3}
@ -477,12 +488,12 @@ while_loop:
} }
for_loop: for_loop:
For ass Down? To expr option(step_clause) block { For var_ass Down? To expr option(step_clause) block {
let region = cover $1 $7.region in let region = cover $1 $7.region in
let value = let value =
{ {
kwd_for = $1; kwd_for = $1;
ass = $2; var_ass = $2;
down = $3; down = $3;
kwd_to = $4; kwd_to = $4;
bound = $5; bound = $5;
@ -671,6 +682,8 @@ core_expr:
| empty_set { SetExpr (EmptySet $1) } | empty_set { SetExpr (EmptySet $1) }
| none_expr { ConstrExpr (NoneExpr $1) } | none_expr { ConstrExpr (NoneExpr $1) }
| fun_call { FunCall $1 } | fun_call { FunCall $1 }
| map_selection { MapLookUp $1 }
| record_expr { RecordExpr $1 }
| Constr arguments { | Constr arguments {
let region = cover $1.region $2.region in let region = cover $1.region $2.region in
ConstrExpr (ConstrApp {region; value = $1,$2}) ConstrExpr (ConstrApp {region; value = $1,$2})
@ -679,15 +692,16 @@ core_expr:
let region = cover $1 $2.region in let region = cover $1 $2.region in
ConstrExpr (SomeApp {region; value = $1,$2}) ConstrExpr (SomeApp {region; value = $1,$2})
} }
| map_name DOT brackets(expr) {
map_selection:
map_name DOT brackets(expr) {
let region = cover $1.region $3.region in let region = cover $1.region $3.region in
let value = { let value = {
map_name = $1; map_name = $1;
selector = $2; selector = $2;
index = $3} index = $3}
in MapLookUp {region; value} in {region; value}
} }
| record_expr { RecordExpr $1 }
record_expr: record_expr:
record_injection { RecordInj $1 } record_injection { RecordInj $1 }
@ -722,7 +736,8 @@ after_field:
field_or_end: field_or_end:
End { End {
`End $1 } `End $1
}
| field_assignment after_field { | field_assignment after_field {
let fields, term, close = $2 in let fields, term, close = $2 in
`Some ($1, fields, term, close) `Some ($1, fields, term, close)

View File

@ -6,7 +6,6 @@ type w is K of (U of int) (*v * u*)
storage s : w // Line comment storage s : w // Line comment
type i is int; type i is int;
operations o : u; operations o : u;
const pi : int = 314159
const x : v = const x : v =
record record
@ -18,7 +17,7 @@ const x : v =
(* Block comment *) (* Block comment *)
entrypoint g (const l : list (int)) is entrypoint g (const l : list (int)) is
var m : map (int, string) := empty_map;
var y : v := copy x with record bar = 7 end; var y : v := copy x with record bar = 7 end;
function f (const x : int) : int is function f (const x : int) : int is
@ -29,6 +28,7 @@ entrypoint g (const l : list (int)) is
end with y * 2 end with y * 2
begin begin
y.[4] := "hello";
match l with match l with
[] -> null [] -> null
| h#t -> q (h+2) | h#t -> q (h+2)