lang: Move the Arg type into the common interface package

This lets it get used in multiple places.
This commit is contained in:
James Shubin
2021-05-30 17:59:50 -04:00
parent 5fae5cd308
commit 453fe18d7f
4 changed files with 48 additions and 48 deletions

View File

@@ -438,6 +438,22 @@ func (obj *Scope) PullIndexes() ([]Expr, bool) {
return indexes, exists return indexes, exists
} }
// Arg represents a name identifier for a func or class argument declaration and
// is sometimes accompanied by a type. This does not satisfy the Expr interface.
type Arg struct {
Name string
Type *types.Type // nil if unspecified (needs to be solved for)
}
// String returns a short representation of this arg.
func (obj *Arg) String() string {
s := obj.Name
if obj.Type != nil {
s += fmt.Sprintf(" %s", obj.Type.String())
}
return s
}
// Edge is the data structure representing a compiled edge that is used in the // Edge is the data structure representing a compiled edge that is used in the
// lang to express a dependency between two resources and optionally send/recv. // lang to express a dependency between two resources and optionally send/recv.
type Edge struct { type Edge struct {

View File

@@ -1416,14 +1416,14 @@ func TestLexParse0(t *testing.T) {
Prog: []interfaces.Stmt{ Prog: []interfaces.Stmt{
&StmtClass{ &StmtClass{
Name: "x", Name: "x",
Args: []*Arg{}, Args: []*interfaces.Arg{},
Body: &StmtProg{ Body: &StmtProg{
Prog: []interfaces.Stmt{}, Prog: []interfaces.Stmt{},
}, },
}, },
&StmtClass{ &StmtClass{
Name: "y1", Name: "y1",
Args: []*Arg{}, Args: []*interfaces.Arg{},
Body: &StmtProg{ Body: &StmtProg{
Prog: []interfaces.Stmt{}, Prog: []interfaces.Stmt{},
}, },
@@ -1462,7 +1462,7 @@ func TestLexParse0(t *testing.T) {
Prog: []interfaces.Stmt{ Prog: []interfaces.Stmt{
&StmtClass{ &StmtClass{
Name: "c1", Name: "c1",
Args: []*Arg{ Args: []*interfaces.Arg{
{ {
Name: "a", Name: "a",
//Type: &types.Type{}, //Type: &types.Type{},
@@ -1535,7 +1535,7 @@ func TestLexParse0(t *testing.T) {
Prog: []interfaces.Stmt{ Prog: []interfaces.Stmt{
&StmtClass{ &StmtClass{
Name: "c1", Name: "c1",
Args: []*Arg{ Args: []*interfaces.Arg{
{ {
Name: "a", Name: "a",
Type: types.TypeStr, Type: types.TypeStr,
@@ -1727,7 +1727,7 @@ func TestLexParse0(t *testing.T) {
&StmtFunc{ &StmtFunc{
Name: "f1", Name: "f1",
Func: &ExprFunc{ Func: &ExprFunc{
Args: []*Arg{}, Args: []*interfaces.Arg{},
Body: &ExprInt{ Body: &ExprInt{
V: 42, V: 42,
}, },
@@ -1748,7 +1748,7 @@ func TestLexParse0(t *testing.T) {
} }
{ {
fn := &ExprFunc{ fn := &ExprFunc{
Args: []*Arg{}, Args: []*interfaces.Arg{},
Return: types.TypeInt, Return: types.TypeInt,
Body: &ExprCall{ Body: &ExprCall{
Name: operatorFuncName, Name: operatorFuncName,
@@ -1790,7 +1790,7 @@ func TestLexParse0(t *testing.T) {
} }
{ {
fn := &ExprFunc{ fn := &ExprFunc{
Args: []*Arg{ Args: []*interfaces.Arg{
{ {
Name: "a", Name: "a",
Type: types.TypeInt, Type: types.TypeInt,
@@ -1841,7 +1841,7 @@ func TestLexParse0(t *testing.T) {
} }
{ {
fn := &ExprFunc{ fn := &ExprFunc{
Args: []*Arg{ Args: []*interfaces.Arg{
{ {
Name: "x", Name: "x",
Type: types.TypeStr, Type: types.TypeStr,
@@ -1888,7 +1888,7 @@ func TestLexParse0(t *testing.T) {
{ {
fn := &ExprFunc{ fn := &ExprFunc{
Args: []*Arg{}, Args: []*interfaces.Arg{},
Body: &ExprInt{ Body: &ExprInt{
V: 42, V: 42,
}, },
@@ -1915,7 +1915,7 @@ func TestLexParse0(t *testing.T) {
} }
{ {
fn := &ExprFunc{ fn := &ExprFunc{
Args: []*Arg{ Args: []*interfaces.Arg{
{ {
Name: "x", Name: "x",
Type: types.TypeStr, Type: types.TypeStr,
@@ -1962,7 +1962,7 @@ func TestLexParse0(t *testing.T) {
} }
{ {
fn := &ExprFunc{ fn := &ExprFunc{
Args: []*Arg{ Args: []*interfaces.Arg{
{ {
Name: "x", Name: "x",
Type: types.TypeStr, Type: types.TypeStr,
@@ -2027,10 +2027,10 @@ func TestLexParse0(t *testing.T) {
Name: "funcgen", Name: "funcgen",
// This is the outer function... // This is the outer function...
Func: &ExprFunc{ Func: &ExprFunc{
Args: []*Arg{}, Args: []*interfaces.Arg{},
// This is the inner function... // This is the inner function...
Body: &ExprFunc{ Body: &ExprFunc{
Args: []*Arg{}, Args: []*interfaces.Arg{},
Body: &ExprStr{ Body: &ExprStr{
V: "hello", V: "hello",
}, },

View File

@@ -62,8 +62,8 @@ func init() {
structFields []*ExprStructField structFields []*ExprStructField
structField *ExprStructField structField *ExprStructField
args []*Arg args []*interfaces.Arg
arg *Arg arg *interfaces.Arg
resContents []StmtResContents // interface resContents []StmtResContents // interface
resField *StmtResField resField *StmtResField
@@ -828,7 +828,7 @@ args:
/* end of list */ /* end of list */
{ {
posLast(yylex, yyDollar) // our pos posLast(yylex, yyDollar) // our pos
$$.args = []*Arg{} $$.args = []*interfaces.Arg{}
} }
| args COMMA arg | args COMMA arg
{ {
@@ -838,21 +838,21 @@ args:
| arg | arg
{ {
posLast(yylex, yyDollar) // our pos posLast(yylex, yyDollar) // our pos
$$.args = append([]*Arg{}, $1.arg) $$.args = append([]*interfaces.Arg{}, $1.arg)
} }
; ;
arg: arg:
// `$x` // `$x`
VAR_IDENTIFIER VAR_IDENTIFIER
{ {
$$.arg = &Arg{ $$.arg = &interfaces.Arg{
Name: $1.str, Name: $1.str,
} }
} }
// `$x <type>` // `$x <type>`
| VAR_IDENTIFIER type | VAR_IDENTIFIER type
{ {
$$.arg = &Arg{ $$.arg = &interfaces.Arg{
Name: $1.str, Name: $1.str,
Type: $2.typ, Type: $2.typ,
} }
@@ -1242,7 +1242,7 @@ type_struct_fields:
/* end of list */ /* end of list */
{ {
posLast(yylex, yyDollar) // our pos posLast(yylex, yyDollar) // our pos
$$.args = []*Arg{} $$.args = []*interfaces.Arg{}
} }
| type_struct_fields SEMICOLON type_struct_field | type_struct_fields SEMICOLON type_struct_field
{ {
@@ -1252,14 +1252,14 @@ type_struct_fields:
| type_struct_field | type_struct_field
{ {
posLast(yylex, yyDollar) // our pos posLast(yylex, yyDollar) // our pos
$$.args = append([]*Arg{}, $1.arg) $$.args = append([]*interfaces.Arg{}, $1.arg)
} }
; ;
type_struct_field: type_struct_field:
IDENTIFIER type IDENTIFIER type
{ {
posLast(yylex, yyDollar) // our pos posLast(yylex, yyDollar) // our pos
$$.arg = &Arg{ // re-use the Arg struct $$.arg = &interfaces.Arg{ // re-use the Arg struct
Name: $1.str, Name: $1.str,
Type: $2.typ, Type: $2.typ,
} }
@@ -1269,7 +1269,7 @@ type_func_args:
/* end of list */ /* end of list */
{ {
posLast(yylex, yyDollar) // our pos posLast(yylex, yyDollar) // our pos
$$.args = []*Arg{} $$.args = []*interfaces.Arg{}
} }
| type_func_args COMMA type_func_arg | type_func_args COMMA type_func_arg
{ {
@@ -1279,15 +1279,15 @@ type_func_args:
| type_func_arg | type_func_arg
{ {
posLast(yylex, yyDollar) // our pos posLast(yylex, yyDollar) // our pos
$$.args = append([]*Arg{}, $1.arg) $$.args = append([]*interfaces.Arg{}, $1.arg)
//$$.args = []*Arg{$1.arg} // TODO: is this equivalent? //$$.args = []*interfaces.Arg{$1.arg} // TODO: is this equivalent?
} }
; ;
type_func_arg: type_func_arg:
// `<type>` // `<type>`
type type
{ {
$$.arg = &Arg{ $$.arg = &interfaces.Arg{
Type: $1.typ, Type: $1.typ,
} }
} }
@@ -1295,7 +1295,7 @@ type_func_arg:
// XXX: should we allow specifying the arg name here? // XXX: should we allow specifying the arg name here?
| VAR_IDENTIFIER type | VAR_IDENTIFIER type
{ {
$$.arg = &Arg{ $$.arg = &interfaces.Arg{
Name: $1.str, Name: $1.str,
Type: $2.typ, Type: $2.typ,
} }

View File

@@ -3970,7 +3970,7 @@ type StmtClass struct {
scope *interfaces.Scope // store for referencing this later scope *interfaces.Scope // store for referencing this later
Name string Name string
Args []*Arg Args []*interfaces.Arg
Body interfaces.Stmt // probably a *StmtProg Body interfaces.Stmt // probably a *StmtProg
} }
@@ -4008,7 +4008,7 @@ func (obj *StmtClass) Interpolate() (interfaces.Stmt, error) {
args := obj.Args args := obj.Args
if obj.Args == nil { if obj.Args == nil {
args = []*Arg{} args = []*interfaces.Arg{}
} }
return &StmtClass{ return &StmtClass{
@@ -4032,7 +4032,7 @@ func (obj *StmtClass) Copy() (interfaces.Stmt, error) {
args := obj.Args args := obj.Args
if obj.Args == nil { if obj.Args == nil {
args = []*Arg{} args = []*interfaces.Arg{}
} }
if !copied { // it's static if !copied { // it's static
@@ -6507,7 +6507,7 @@ type ExprFunc struct {
// Args are the list of args that were used when defining the function. // Args are the list of args that were used when defining the function.
// This can include a string name and a type, however the type might be // This can include a string name and a type, however the type might be
// absent here. // absent here.
Args []*Arg Args []*interfaces.Arg
// Return is the return type of the function if it was defined. // Return is the return type of the function if it was defined.
Return *types.Type // return type if specified Return *types.Type // return type if specified
// Body is the contents of the function. It can be any expression. // Body is the contents of the function. It can be any expression.
@@ -6647,7 +6647,7 @@ func (obj *ExprFunc) Interpolate() (interfaces.Expr, error) {
args := obj.Args args := obj.Args
if obj.Args == nil { if obj.Args == nil {
args = []*Arg{} args = []*interfaces.Arg{}
} }
return &ExprFunc{ return &ExprFunc{
@@ -8600,22 +8600,6 @@ func (obj *ExprVar) Value() (types.Value, error) {
return expr.Value() // recurse return expr.Value() // recurse
} }
// Arg represents a name identifier for a func or class argument declaration and
// is sometimes accompanied by a type. This does not satisfy the Expr interface.
type Arg struct {
Name string
Type *types.Type // nil if unspecified (needs to be solved for)
}
// String returns a short representation of this arg.
func (obj *Arg) String() string {
s := obj.Name
if obj.Type != nil {
s += fmt.Sprintf(" %s", obj.Type.String())
}
return s
}
// ExprIf represents an if expression which *must* have both branches, and which // ExprIf represents an if expression which *must* have both branches, and which
// returns a value. As a result, it has a type. This is different from a StmtIf, // returns a value. As a result, it has a type. This is different from a StmtIf,
// which does not need to have both branches, and which does not return a value. // which does not need to have both branches, and which does not return a value.