Skip to content

Commit

Permalink
Update identifiers grammar, add constant $, fix :, add placeholder _
Browse files Browse the repository at this point in the history
  • Loading branch information
engboris committed Jun 15, 2024
1 parent 8c39563 commit 83e0228
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
open Parser
}

let var_id = ['A'-'Z'] ['A'-'Z' '0'-'9']*
let func_id = ['a'-'z' '0'-'9' '_']+
let var_id = ['A'-'Z'] ['A'-'Z' '0'-'9' '_' '-']* '\''*
let func_id = ['a'-'z' '0'-'9'] ['a'-'z' 'A'-'Z' '0'-'9' '_' '-']* '\''* '?'?
let space = [' ' '\t']+
let newline = '\r' | '\n' | "\r\n"

Expand All @@ -12,6 +12,7 @@ rule read = parse
| func_id { SYM (Lexing.lexeme lexbuf) }
| '\'' { comment lexbuf }
| "'''" { comments lexbuf }
| '_' { PLACEHOLDER }
| '[' { LEFT_BRACK }
| ']' { RIGHT_BRACK }
| '(' { LEFT_PAR }
Expand All @@ -20,6 +21,7 @@ rule read = parse
| '@' { AT }
| '+' { PLUS }
| '-' { MINUS }
| '$' { EMPTY_SYM }
| ':' { CONS }
| ';' { SEMICOLON }
| space { read lexbuf }
Expand Down
4 changes: 4 additions & 0 deletions src/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
%token CONS
%token AT
%token SEMICOLON
%token PLACEHOLDER
%token EMPTY_SYM
%token EOF

%right CONS
Expand All @@ -33,6 +35,8 @@ symbol:
| f = SYM { (Stellar.Null, f) }

ray:
| EMPTY_SYM { Stellar.to_func ((Stellar.Null, "$"), []) }
| PLACEHOLDER { Stellar.to_var ("_"^(Stellar.fresh_placeholder ())) }
| x = VAR { Stellar.to_var x }
| r1 = ray; CONS; r2 = ray { Stellar.to_func ((Stellar.Null, ":"), [r1; r2]) }
| pf = symbol; LEFT_PAR; ts = separated_nonempty_list(COMMA?, ray); RIGHT_PAR
Expand Down
21 changes: 14 additions & 7 deletions src/stellar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ open StellarRays
Stars and Constellations
--------------------------------------- *)

let counter_placeholder = ref 0
let fresh_placeholder () =
let r = !counter_placeholder in
(counter_placeholder := !counter_placeholder + 1);
(Int.to_string r)

type ray = term
type star = ray list
type constellation = star list
Expand Down Expand Up @@ -58,17 +64,16 @@ let is_polarised r : bool =
| (Null, _) -> false
in exists_func aux r

let is_prefixed ~by:_ : ray -> bool = function
| Func (_, _) -> true
| _ -> false

(* ---------------------------------------
Pretty Printer
--------------------------------------- *)

let rec string_of_ray = function
| Var x -> x
| Func (pf, []) -> string_of_polsym pf
| Func ((Null, ":"), [Func ((Null, ":"), [r1; r2]); r3]) ->
"(" ^ (string_of_ray r1) ^ ":" ^ (string_of_ray r2) ^ "):" ^
(string_of_ray r3)
| Func ((Null, ":"), [r1; r2]) ->
(string_of_ray r1) ^ ":" ^ (string_of_ray r2)
| Func (pf, ts) -> string_of_polsym pf ^
Expand Down Expand Up @@ -199,11 +204,13 @@ let interaction ?(withloops=true) ?(showtrace=false) ?(selfint=false) cs space =
output_string stdout "\n";
end;
let new_stars =
search_partners ~withloops ~showtrace (r, accr@s') (cs@space')
|>
if selfint then
self_interaction ~withloops ~showtrace (r, accr@s') @
search_partners ~withloops ~showtrace (r, accr@s') cs
List.append (self_interaction ~withloops ~showtrace (r, accr@s'))
else
search_partners ~withloops ~showtrace (r, accr@s') cs in
Fn.id
in
if List.is_empty new_stars then select_ray (r::accr) s'
else Some new_stars
in let new_stars = select_ray [] s in
Expand Down

0 comments on commit 83e0228

Please sign in to comment.