Select the types of activity you want to include in your feed.
While loops and arithmetic expressions
Thanks to Ishaan Ghandi's work in https://github.com/colis-anr/morbig/pull/120, we now have some limited support for arithmetic expression handling in the shell. Nothing very extravagant, but workable.
···11+(* We handle _very_ simple arithmetic expressions.
22+ Really nothing crazy yet, hopefully enough to handle
33+ most [while x < 10 do x = x + 1 done] loops! *)
44+55+type expr =
66+ | Int of int
77+ | Var of string
88+ | Add of expr * expr
99+ | Sub of expr * expr
1010+ | Mul of expr * expr
1111+ | Div of expr * expr
1212+ | Neg of expr
1313+[@@deriving to_yojson]
1414+1515+let eval lookup expr =
1616+ let rec calc = function
1717+ | Int i -> i
1818+ | Var v -> lookup v
1919+ | Add (e1, e2) -> calc e1 + calc e2
2020+ | Sub (e1, e2) -> calc e1 - calc e2
2121+ | Div (e1, e2) -> calc e1 / calc e2
2222+ | Mul (e1, e2) -> calc e1 * calc e2
2323+ | Neg n -> Int.neg (calc n)
2424+ in
2525+ calc expr
+31
src/lib/arith_lexer.mll
···11+{
22+ open Arith_parser
33+}
44+55+let digit = ['0'-'9']
66+let alpha = ['a'-'z' 'A'-'Z' '_']
77+let ident = alpha (alpha | digit)*
88+let var =
99+ ident
1010+| '$' ident
1111+1212+rule read = parse
1313+ | [' ' '\t' '\n'] { read lexbuf }
1414+1515+ | '+' { PLUS }
1616+ | '-' { MINUS }
1717+ | '*' { STAR }
1818+ | '/' { SLASH }
1919+2020+ | '(' { LPAREN }
2121+ | ')' { RPAREN }
2222+2323+ | digit+ as i { INT (int_of_string i) }
2424+2525+ | var as v { VAR v }
2626+2727+ | eof { EOF }
2828+2929+ | _ as c
3030+ { failwith ("Unexpected character: " ^ Char.escaped c) }
3131+
+34
src/lib/arith_parser.mly
···11+%{
22+ open Arith
33+%}
44+55+%token <int> INT
66+%token <string> VAR
77+%token PLUS MINUS STAR SLASH
88+%token LPAREN RPAREN
99+%token EOF
1010+1111+%left PLUS MINUS
1212+%left STAR SLASH
1313+%right UMINUS UPLUS
1414+1515+%start <Arith.expr> main
1616+1717+%%
1818+1919+main:
2020+ | expr EOF { $1 }
2121+2222+expr:
2323+ | expr PLUS expr { Add ($1, $3) }
2424+ | expr MINUS expr { Sub ($1, $3) }
2525+ | expr STAR expr { Mul ($1, $3) }
2626+ | expr SLASH expr { Div ($1, $3) }
2727+2828+ | PLUS expr %prec UPLUS { $2 }
2929+ | MINUS expr %prec UMINUS { Neg $2 }
3030+3131+ | INT { Int $1 }
3232+ | VAR { Var $1 }
3333+ | LPAREN expr RPAREN { $2 }
3434+
+1
src/lib/ast.ml
···539539 WordVariable a
540540 | WordGlobAll -> WordGlobAll
541541 | WordGlobAny -> WordGlobAny
542542+ | WordArithmeticExpression s -> WordArithmeticExpression (word s)
542543 | WordReBracketExpression a ->
543544 let a = bracket_expression a in
544545 WordReBracketExpression a
···328328 | WordVariable of variable
329329 | WordGlobAll (* asterisk *)
330330 | WordGlobAny (* question mark *)
331331+ | WordArithmeticExpression of word
331332 | WordReBracketExpression of bracket_expression
332333 (* Empty CST. Useful to represent the absence of relevant CSTs. *)
333334 | WordEmpty
+9-2
vendor/morbig.0.11.0/src/prelexer.mll
···431431 }
432432433433| "$((" {
434434- let current = push_string current "$((" in
434434+ debug ~rule:"arithmetic-exp" lexbuf current;
435435+ let current = push_arith current in
435436 let current = next_double_rparen 1 current lexbuf in
436437 token current lexbuf
437438 }
···718719 let current = push_string current "((" in
719720 next_double_rparen (dplevel+1) current lexbuf
720721 }
722722+| "$((" {
723723+ debug ~rule:"arithmetic-exp" lexbuf current;
724724+ let current = push_arith current in
725725+ let current = next_double_rparen (dplevel+1) current lexbuf in
726726+ current
727727+ }
721728 | '`' as op | "$" ( '(' as op) {
722729 let escaping_level = 0 in (* FIXME: Probably wrong. *)
723730 let current = push_string current (Lexing.lexeme lexbuf) in
···727734 next_double_rparen dplevel current lexbuf
728735 }
729736 | "))" {
730730- let current = push_string current "))" in
737737+ let current = pop_arith current in
731738 if dplevel = 1
732739 then current
733740 else if dplevel > 1 then next_double_rparen (dplevel-1) current lexbuf
+28
vendor/morbig.0.11.0/src/prelexerState.ml
···2222 | WordComponent of (string * word_component)
2323 | QuotingMark of quote_kind
2424 | AssignmentMark
2525+ | ArithmeticMark
25262627and quote_kind = SingleQuote | DoubleQuote | OpeningBrace
2728···218219 | WordComponent (s, _) -> s
219220 | AssignmentMark -> "|=|"
220221 | QuotingMark _ -> "|Q|"
222222+ | ArithmeticMark -> "|+|"
223223+224224+let push_arith b =
225225+ let cst = ArithmeticMark in
226226+ let buffer = AtomBuffer.make (cst :: buffer b) in
227227+ { b with buffer }
228228+229229+let pop_arith b =
230230+ let rec aux str_expression expression = function
231231+ | [] ->
232232+ (str_expression, expression, [])
233233+ | ArithmeticMark :: buffer -> (str_expression, expression, buffer)
234234+ | (AssignmentMark | QuotingMark _ ) :: buffer ->
235235+ aux str_expression expression buffer (* FIXME: Check twice. *)
236236+ | WordComponent (w, WordEmpty) :: buffer ->
237237+ aux (w ^ str_expression) expression buffer
238238+ | WordComponent (w, c) :: buffer ->
239239+ aux (w ^ str_expression) (c :: expression) buffer
240240+ in
241241+ let str_expression, expression, buffer = aux "" [] (buffer b) in
242242+ let word = Word (str_expression, expression) in
243243+ let expression = WordArithmeticExpression word in
244244+ let str_expression = "$((" ^ str_expression ^ "))"
245245+ in
246246+ let expression = WordComponent (str_expression, expression) in
247247+ let buffer = AtomBuffer.make (expression :: buffer) in
248248+ { b with buffer }
221249222250let contents_of_atom_list atoms =
223251 String.concat "" (List.rev_map string_of_atom atoms)