Skip to content

Commit

Permalink
Add expression variables, solution for #2
Browse files Browse the repository at this point in the history
  • Loading branch information
osimon8 committed Sep 4, 2023
1 parent 470f68b commit 871a42f
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 15 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,37 @@ Temporary variables for use in the code can be defined with the following syntax

<type> <name> = <expression>

Variables used in expressions must have already been defined earlier in the code before they can be used. Duplicate variable names are not allowed. Variables cannot be reassigned after creation (with the exception of the loop variable generated by for loops, which are automatically reassigned throughout the loop).

The supported types are:

- int
- signal
- condition
- circuit
- expression

`int` types are numeric expressions, and `signal` types are signals.

`condition` types are numeric expressions that can directly map to a decider combinator's config. This essentially means that it must be an expression of the form `<signal> <c_op> <expression>`, where `c_op` is either `==`, `!=`, `>`, `>=`, `<`, or `<=`, and `expression` is a numeric expression without any signals.

`circuit` types are circuits, which can be the name of a circuit that was previously bound, a pattern call, or a `for` expression.

Variables used in expressions must have already been defined earlier in the code before they can be used. Duplicate variable names are not allowed.

`expression` types are simply any expression that serve as a sort of copy-paste shorthand for reusing the same block of logic. For example, the following two code snippets are equivalent:

```
circuit c1 : D = (A + B - C) / 45;
circuit c2 : E = (A + B - C) - (G * H);
```

with an `expression` variable:

```
expression expr = A + B + C;
circuit c1 : D = expr / 45;
circuit c2 : E = expr - (G * H);
```

### Output

Expand Down
2 changes: 1 addition & 1 deletion combc.opam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.2.10"
version: "0.2.11"
synopsis: "C-style language that compiles to Factorio combinators"
maintainer: ["Owen Simon <osimon922@gmail.com>"]
authors: ["Owen Simon <osimon922@gmail.com>"]
Expand Down
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(lang dune 3.0)
(using menhir 2.1)
(name combc)
(version 0.2.10)
(version 0.2.11)
(license "GPL-3.0")
(authors "Owen Simon <osimon922@gmail.com>")
(maintainers "Owen Simon <osimon922@gmail.com>")
Expand Down
3 changes: 3 additions & 0 deletions src/ast/expression.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ open Circuit

type var_type =
| TInt
| TStamp
| TCondition
| TSignal
| TCircuit
Expand All @@ -11,6 +12,7 @@ type var_type =
let string_of_type ty : string =
begin match ty with
| TInt -> "int"
| TStamp -> "expression"
| TCondition -> "condition"
| TSignal -> "signal"
| TCircuit -> "circuit"
Expand Down Expand Up @@ -48,6 +50,7 @@ type compiled_circuit =
type expression =
| Call of string * delayed_expression list
| Int of int32
| Stamp of bexp
| Condition of bexp
| Var of string
| Signal of string
Expand Down
24 changes: 14 additions & 10 deletions src/compiler/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ let json_of_bexp (o_sig:symbol) (b: bexp) : json list =
let interpret_type exp : var_type * expression =
begin match exp with
| Int _ -> TInt
| Stamp _ -> TStamp
| Condition _ -> TCondition
| Signal _ -> TSignal
| Circuit _
Expand All @@ -207,16 +208,16 @@ let rec bottom_out_var e : expression =
end


let expression_of_delayed exp =
let expression_of_delayed (exp: delayed_expression) : expression =
begin match exp with
| Immediate exp -> exp
| Delayed bexp ->
let bexp = bind_vars_of_bexp bexp in
begin match expression_of_bexp bexp with
| Immediate exp -> exp
| Delayed _ -> Circuit (Inline (bexp, "check", None))
end
end
| Immediate exp -> exp
| Delayed bexp ->
let bexp = bind_vars_of_bexp bexp in
begin match expression_of_bexp bexp with
| Immediate exp -> exp
| Delayed _ -> Circuit (Inline (bexp, "check", None))
end
end

let rec evaluate_expression (expression:expression) : expression =
let rec inter exp =
Expand Down Expand Up @@ -253,6 +254,7 @@ let rec evaluate_expression (expression:expression) : expression =
Circuit (Compiled c)
| Signal _
| Int _
| Stamp _
| Circuit _
| Pattern _ -> exp
end in
Expand Down Expand Up @@ -291,6 +293,7 @@ and compile_ctree_to_circuit (ctree:ctree) : compiled_circuit =
begin match e with
| Signal _
| Condition _
| Stamp _
| Int _ -> Abstract (compile_bexp_to_circuit o_sig ast)
| Circuit c -> compile_ctree_to_circuit c
| _ -> prerr_endline "Cannot bind variable to circuit. This error should never occur"; exit 1
Expand Down Expand Up @@ -377,7 +380,8 @@ and evaluate_expression_to_ctree exp loc : compiled_circuit =
begin match exp with
| Signal s -> Inline (Signal s, s, loc)
| Int i -> Inline (Lit i, "signal-check", loc)
| Condition b -> Inline (b, "signal-check", loc)
| Condition b
| Stamp b -> Inline (b, "signal-check", loc)
| Circuit c -> c
| Pattern _ -> prerr_endline "Can't evaluate pattern to circuit. Did you mean to provide arguments?"; exit 1
| _ -> failwith "Expression not reduced! Impossible error"
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/firstPhase.ml
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ let bind_vars_of_bexp bexp : bexp =
| Var var ->
let ty, v = Ctxt.lookup var in
begin match v with
| Int i -> Lit i
| Int x -> Lit x
| Signal s -> Signal s
| Condition b -> i b
| Condition b
| Stamp b -> i b
| Var v -> i (Var v)
| _ -> prerr_endline (Printf.sprintf "Can't bind variable \"%s\" of type \"%s\" to expression" var (Ast.Expression.string_of_type ty)) ; exit 1
end
Expand Down
1 change: 1 addition & 0 deletions src/parser/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
("int", TINT);
("signal", TSIGNAL);
("condition", TCONDITION);
("expression", TSTAMP);

("{", LBRACE);
("}", RBRACE);
Expand Down
2 changes: 2 additions & 0 deletions src/parser/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ open Compiler.Directive;;
%token TINT
%token TSIGNAL
%token TCONDITION
%token TSTAMP

%start toplevel

Expand Down Expand Up @@ -126,6 +127,7 @@ command:

| TINT i=IDENT ASSIGN b=bexp SEMI { Assign(i, TInt, expression_of_bexp b) }
| TCONDITION i=IDENT ASSIGN b=bexp SEMI { Assign(i, TCondition, expression_of_bexp b) }
| TSTAMP i=IDENT ASSIGN b=bexp SEMI { Assign(i, TStamp, Immediate (Stamp b)) }
| TSIGNAL i=IDENT ASSIGN b=bexp SEMI { Assign(i, TSignal, expression_of_bexp b) }
| o=output SEMI { o }

Expand Down

0 comments on commit 871a42f

Please sign in to comment.