Skip to content

Commit

Permalink
Refactor Variable and Parameter structs
Browse files Browse the repository at this point in the history
This commit renames the Variable and Parameter structs to VarDeclaration and ParamDeclaration which better match the grammar and the role of these objects. The commit also slightly refactor VarDeclaration to group the variable's type and its domain in a single Variable struct. That struct will be useful to add support for predicate parsing in a future commit.
  • Loading branch information
rhartert committed May 27, 2024
1 parent c48ae9d commit 829c646
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 116 deletions.
22 changes: 11 additions & 11 deletions fzn/fzn.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (

// Model represents a FlatZinc model.
type Model struct {
Predicates []Predicate
Parameters []Parameter
Variables []Variable
Constraints []Constraint
SolveGoals []SolveGoal
Predicates []Predicate
ParamDeclarations []ParamDeclaration
VarDeclarations []VarDeclaration
Constraints []Constraint
SolveGoals []SolveGoal
}

// ParseModel reads a FlatZinc model from the provided reader and returns a
Expand All @@ -36,8 +36,8 @@ func ParseModel(reader io.Reader) (*Model, error) {
// components of a FlatZinc model.
type Handler interface {
AddPredicate(p *Predicate) error
AddParameter(p *Parameter) error
AddVariable(v *Variable) error
AddParamDeclaration(p *ParamDeclaration) error
AddVarDeclaration(v *VarDeclaration) error
AddConstraint(c *Constraint) error
AddSolveGoal(s *SolveGoal) error
}
Expand Down Expand Up @@ -89,13 +89,13 @@ func (mb *modelBuilder) AddPredicate(p *Predicate) error {
return nil
}

func (mb *modelBuilder) AddParameter(p *Parameter) error {
mb.Model.Parameters = append(mb.Model.Parameters, *p)
func (mb *modelBuilder) AddParamDeclaration(p *ParamDeclaration) error {
mb.Model.ParamDeclarations = append(mb.Model.ParamDeclarations, *p)
return nil
}

func (mb *modelBuilder) AddVariable(v *Variable) error {
mb.Model.Variables = append(mb.Model.Variables, *v)
func (mb *modelBuilder) AddVarDeclaration(v *VarDeclaration) error {
mb.Model.VarDeclarations = append(mb.Model.VarDeclarations, *v)
return nil
}

Expand Down
34 changes: 20 additions & 14 deletions fzn/fzn_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ solve maximize X_INTRODUCED_0_;
`

var testCakesModel = Model{
Parameters: []Parameter{
ParamDeclarations: []ParamDeclaration{
{
Identifier: "X_INTRODUCED_2_",
Array: &Array{Start: 1, End: 2},
Type: ParTypeInt,
Exprs: []Literal{
Literals: []Literal{
{Int: ptr.Of(250)},
{Int: ptr.Of(200)},
},
Expand All @@ -38,7 +38,7 @@ var testCakesModel = Model{
Identifier: "X_INTRODUCED_6_",
Array: &Array{Start: 1, End: 2},
Type: ParTypeInt,
Exprs: []Literal{
Literals: []Literal{
{Int: ptr.Of(75)},
{Int: ptr.Of(150)},
},
Expand All @@ -47,29 +47,35 @@ var testCakesModel = Model{
Identifier: "X_INTRODUCED_8_",
Array: &Array{Start: 1, End: 2},
Type: ParTypeInt,
Exprs: []Literal{
Literals: []Literal{
{Int: ptr.Of(100)},
{Int: ptr.Of(150)},
},
},
},
Variables: []Variable{
VarDeclarations: []VarDeclaration{
{
Identifier: "b",
Type: VarTypeIntRange,
Domain: VarDomain{IntDomain: &SetIntLit{Values: [][]int{{0, 3}}}},
Identifier: "b",
Variable: Variable{
Type: VarTypeIntRange,
IntDomain: &SetIntLit{Values: [][]int{{0, 3}}},
},
Annotations: []Annotation{{Identifier: "output_var"}},
},
{
Identifier: "c",
Type: VarTypeIntRange,
Domain: VarDomain{IntDomain: &SetIntLit{Values: [][]int{{0, 6}}}},
Identifier: "c",
Variable: Variable{
Type: VarTypeIntRange,
IntDomain: &SetIntLit{Values: [][]int{{0, 6}}},
},
Annotations: []Annotation{{Identifier: "output_var"}},
},
{
Identifier: "X_INTRODUCED_0_",
Type: VarTypeIntRange,
Domain: VarDomain{IntDomain: &SetIntLit{Values: [][]int{{0, 85000}}}},
Identifier: "X_INTRODUCED_0_",
Variable: Variable{
Type: VarTypeIntRange,
IntDomain: &SetIntLit{Values: [][]int{{0, 85000}}},
},
Annotations: []Annotation{{Identifier: "is_defined_var"}},
},
},
Expand Down
112 changes: 63 additions & 49 deletions fzn/fzn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ import (
// instruction implements the Handler interface and serves as a convenient way
// to compare parsed instructions.
type instruction struct {
Predicate *Predicate
Parameter *Parameter
Variable *Variable
Constraint *Constraint
SolveGoal *SolveGoal
Predicate *Predicate
ParamDeclaration *ParamDeclaration
VarDeclaration *VarDeclaration
Constraint *Constraint
SolveGoal *SolveGoal
}

func (i *instruction) AddPredicate(p *Predicate) error {
i.Predicate = p
return nil
}

func (i *instruction) AddParameter(p *Parameter) error {
i.Parameter = p
func (i *instruction) AddParamDeclaration(p *ParamDeclaration) error {
i.ParamDeclaration = p
return nil
}

func (i *instruction) AddVariable(v *Variable) error {
i.Variable = v
func (i *instruction) AddVarDeclaration(v *VarDeclaration) error {
i.VarDeclaration = v
return nil
}

Expand Down Expand Up @@ -134,40 +134,40 @@ func TestParse_parameter(t *testing.T) {
{
input: "int: foo = 42;",
want: instruction{
Parameter: &Parameter{
ParamDeclaration: &ParamDeclaration{
Identifier: "foo",
Type: ParTypeInt,
Exprs: []Literal{{Int: ptr.Of(42)}},
Literals: []Literal{{Int: ptr.Of(42)}},
},
},
},
{
input: "bool: foo = true;",
want: instruction{
Parameter: &Parameter{
ParamDeclaration: &ParamDeclaration{
Identifier: "foo",
Type: ParTypeBool,
Exprs: []Literal{{Bool: ptr.Of(true)}},
Literals: []Literal{{Bool: ptr.Of(true)}},
},
},
},
{
input: "float: foo = 42.0;",
want: instruction{
Parameter: &Parameter{
ParamDeclaration: &ParamDeclaration{
Identifier: "foo",
Type: ParTypeFloat,
Exprs: []Literal{{Float: ptr.Of(42.0)}},
Literals: []Literal{{Float: ptr.Of(42.0)}},
},
},
},
{
input: "set of int: foo = {42, 44, 45};",
want: instruction{
Parameter: &Parameter{
ParamDeclaration: &ParamDeclaration{
Identifier: "foo",
Type: ParTypeSetOfInt,
Exprs: []Literal{{SetInt: &SetIntLit{
Literals: []Literal{{SetInt: &SetIntLit{
Values: [][]int{{42, 42}, {44, 45}},
}}},
},
Expand All @@ -176,11 +176,11 @@ func TestParse_parameter(t *testing.T) {
{
input: "array [1..2] of int: foo = [42, 1337];",
want: instruction{
Parameter: &Parameter{
ParamDeclaration: &ParamDeclaration{
Identifier: "foo",
Type: ParTypeInt,
Array: &Array{1, 2},
Exprs: []Literal{
Literals: []Literal{
{Int: ptr.Of(42)},
{Int: ptr.Of(1337)},
},
Expand Down Expand Up @@ -215,93 +215,107 @@ func TestParse_variables(t *testing.T) {
{
input: "var int: X;",
want: instruction{
Variable: &Variable{
VarDeclaration: &VarDeclaration{
Identifier: "X",
Type: VarTypeIntRange,
Domain: VarDomain{},
Variable: Variable{
Type: VarTypeIntRange,
},
},
},
},
{
input: "var int : X ::foo;",
want: instruction{
Variable: &Variable{
Identifier: "X",
Type: VarTypeIntRange,
Domain: VarDomain{},
VarDeclaration: &VarDeclaration{
Identifier: "X",
Variable: Variable{
Type: VarTypeIntRange,
},
Annotations: []Annotation{{Identifier: "foo"}},
},
},
},
{
input: "var 1..5: X;",
want: instruction{
Variable: &Variable{
VarDeclaration: &VarDeclaration{
Identifier: "X",
Type: VarTypeIntRange,
Domain: VarDomain{IntDomain: &SetIntLit{Values: [][]int{{1, 5}}}},
Variable: Variable{
Type: VarTypeIntRange,
IntDomain: &SetIntLit{Values: [][]int{{1, 5}}},
},
},
},
},
{
input: "var 0.1..0.5: X;",
want: instruction{
Variable: &Variable{
VarDeclaration: &VarDeclaration{
Identifier: "X",
Type: VarTypeFloatRange,
Domain: VarDomain{FloatDomain: &SetFloatLit{Values: [][]float64{{0.1, 0.5}}}},
Variable: Variable{
Type: VarTypeFloatRange,
FloatDomain: &SetFloatLit{Values: [][]float64{{0.1, 0.5}}},
},
},
},
},
{
input: "var {1, 3}: X;",
want: instruction{
Variable: &Variable{
VarDeclaration: &VarDeclaration{
Identifier: "X",
Type: VarTypeIntSet,
Domain: VarDomain{IntDomain: &SetIntLit{Values: [][]int{{1, 1}, {3, 3}}}},
Variable: Variable{
Type: VarTypeIntSet,
IntDomain: &SetIntLit{Values: [][]int{{1, 1}, {3, 3}}},
},
},
},
},
{
input: "var set of 1..3: X;",
want: instruction{
Variable: &Variable{
VarDeclaration: &VarDeclaration{
Identifier: "X",
Type: VarTypeIntSet,
Domain: VarDomain{IntDomain: &SetIntLit{Values: [][]int{{1, 3}}}},
Variable: Variable{
Type: VarTypeIntSet,
IntDomain: &SetIntLit{Values: [][]int{{1, 3}}},
},
},
},
},
{
input: "var set of {1, 3} : X;",
want: instruction{
Variable: &Variable{
VarDeclaration: &VarDeclaration{
Identifier: "X",
Type: VarTypeIntSet,
Domain: VarDomain{IntDomain: &SetIntLit{Values: [][]int{{1, 1}, {3, 3}}}},
Variable: Variable{
Type: VarTypeIntSet,
IntDomain: &SetIntLit{Values: [][]int{{1, 1}, {3, 3}}},
},
},
},
},
{
input: "array [1..2] of var int: X;",
want: instruction{
Variable: &Variable{
VarDeclaration: &VarDeclaration{
Identifier: "X",
Type: VarTypeIntRange,
Array: &Array{1, 2},
Domain: VarDomain{},
Variable: Variable{
Type: VarTypeIntRange,
},
Array: &Array{1, 2},
},
},
},
{
input: "array [1..2] of var int: X = [foo, bar];",
want: instruction{
Variable: &Variable{
VarDeclaration: &VarDeclaration{
Identifier: "X",
Type: VarTypeIntRange,
Array: &Array{1, 2},
Domain: VarDomain{},
Variable: Variable{
Type: VarTypeIntRange,
},
Array: &Array{1, 2},
Exprs: []BasicExpr{
{Identifier: "foo"},
{Identifier: "bar"},
Expand Down
8 changes: 4 additions & 4 deletions fzn/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// | "array" "[" <index-set> "]" "of" <basic-par-type>
//

func isParameter(p *parser) bool {
func isParamDeclaration(p *parser) bool {
switch p.lookAhead(0).Type {
case tok.IntType:
return true
Expand All @@ -36,8 +36,8 @@ func isParameter(p *parser) bool {
}
}

func parseParameter(p *parser) (param *Parameter, err error) {
param = &Parameter{}
func parseParamDeclaration(p *parser) (param *ParamDeclaration, err error) {
param = &ParamDeclaration{}

if p.lookAhead(0).Type == tok.Array {
param.Array, err = parseArrayOf(p)
Expand All @@ -64,7 +64,7 @@ func parseParameter(p *parser) (param *Parameter, err error) {
return nil, fmt.Errorf("missing assign")
}

param.Exprs, err = parseParamExpr(p)
param.Literals, err = parseParamExpr(p)
if err != nil {
return nil, fmt.Errorf("error parsing parameter expressions: %w", err)
}
Expand Down
Loading

0 comments on commit 829c646

Please sign in to comment.