Skip to content

Commit

Permalink
Add StructType and TypeSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
M. Mert Yildiran committed Feb 12, 2021
1 parent 6fdc8f9 commit a7bcb1f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 12 deletions.
12 changes: 12 additions & 0 deletions gopygo/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,15 @@ def __init__(self, _type: str):
class StarExpr():
def __init__(self, x):
self.x = x


class StructType():
def __init__(self, fields: FieldList, incomplete: bool):
self.fields = fields
self.incomplete = incomplete


class TypeSpec():
def __init__(self, name: Ident, _type):
self.name = name
self.type = _type
46 changes: 37 additions & 9 deletions gopygo/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
RangeStmt,
Ellipsis,
FuncLit,
StarExpr
StarExpr,
StructType,
TypeSpec
)
from gopygo.exceptions import (
LexerError
Expand Down Expand Up @@ -77,6 +79,7 @@ class GoLexer(Lexer):
IF, ELSE,
SWITCH, CASE, DEFAULT,
MAP,
STRUCT,

# Data types
BOOL,
Expand Down Expand Up @@ -128,6 +131,7 @@ class GoLexer(Lexer):
CASE = 'case'
DEFAULT = 'DEFAULT'
MAP = 'map'
STRUCT = 'struct'

# Data types
BOOL = 'bool'
Expand Down Expand Up @@ -339,7 +343,8 @@ def func_type(self, p):
@_(
'',
'field',
'field COMMA field_list'
'field COMMA field_list',
'field NEWLINE field_list'
)
def field_list(self, p):
if len(p) > 2:
Expand All @@ -357,14 +362,20 @@ def field_list(self, p):
'FUNC LPAREN RPAREN _type',
'FUNC LPAREN RPAREN ELLIPSIS _type',
'MUL _type',
'MUL IDENT',
'ELLIPSIS MUL _type',
'IDENT MUL _type',
'IDENT ELLIPSIS MUL _type',
'FUNC LPAREN RPAREN MUL _type',
'FUNC LPAREN RPAREN ELLIPSIS MUL _type',
)
def field(self, p):
_type = StarExpr(p._type) if hasattr(p, 'MUL') else p._type
_type = None
if not hasattr(p, '_type'):
_type = StarExpr(p.IDENT)
else:
_type = StarExpr(p._type) if hasattr(p, 'MUL') else p._type

if hasattr(p, 'FUNC'):
return Field(
None,
Expand All @@ -379,7 +390,7 @@ def field(self, p):
if hasattr(p, 'ELLIPSIS'):
_type = Ellipsis(_type)

if hasattr(p, 'IDENT'):
if hasattr(p, '_type') and hasattr(p, 'IDENT'):
return Field(p.IDENT, _type)
else:
return Field(None, _type)
Expand Down Expand Up @@ -616,18 +627,35 @@ def stmt(self, p):
return ReturnStmt(p[1])

@_(
'TYPE IDENT STRUCT LBRACE field_list RBRACE NEWLINE',
'TYPE IDENT STRUCT LBRACE NEWLINE field_list RBRACE NEWLINE',
'TYPE IDENT STRUCT LBRACE field_list NEWLINE RBRACE NEWLINE',
'TYPE IDENT STRUCT LBRACE NEWLINE field_list NEWLINE RBRACE NEWLINE',
'VAR value_spec NEWLINE',
'CONST value_spec NEWLINE',
'IMPORT value_spec NEWLINE',
'TYPE value_spec NEWLINE',
)
def stmt(self, p):
return DeclStmt(
GenDecl(
p[0],
[p.value_spec]
if hasattr(p, 'IDENT'):
return DeclStmt(
GenDecl(
p[0],
[
TypeSpec(
p.IDENT,
StructType(p.field_list, False)
)
]
)
)
else:
return DeclStmt(
GenDecl(
p[0],
[p.value_spec]
)
)
)

@_(
'BREAK NEWLINE',
Expand Down
25 changes: 22 additions & 3 deletions gopygo/unparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,16 @@ def func_type(self, node):
text += ')'
return text

def field_list(self, node):
def field_list(self, node, separator=', ', indent=''):
text = ''
for field in node.list:
text += '%s, ' % getattr(self, _get_node_type(field))(field)
text += '%s%s%s' % (
indent,
getattr(self, _get_node_type(field))(field),
separator
)
if node.list:
text = text[:-2]
text = text[:-len(separator)]
return text

def field(self, node):
Expand Down Expand Up @@ -374,6 +378,21 @@ def func_lit(self, node):
def star_expr(self, node):
return '*%s' % getattr(self, _get_node_type(node.x))(node.x)

def type_spec(self, node):
return '%s %s' % (
getattr(self, _get_node_type(node.name))(node.name),
getattr(self, _get_node_type(node.type))(node.type)
)

def struct_type(self, node):
return 'struct {\n%s\n}\n' % (
getattr(self, _get_node_type(node.fields))(
node.fields,
separator='\n',
indent=((self.indent + 1) * INDENT)
)
)


def unparse(tree):
generator = Generator()
Expand Down
19 changes: 19 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,25 @@ def test_032_pointers(self):
fmt.Println("zeroptr:", i)
fmt.Println("pointer:", &i)
}
"""
self.parse_unparse()

def test_033_structs(self):
self.program = """
type person struct {
name string
age int
}
type Vertex struct {
X int
Y int
}
type Employee struct {
firstName string
lastName string
age int
salary int
}
"""
self.parse_unparse()

Expand Down

0 comments on commit a7bcb1f

Please sign in to comment.