changeset 6df951dcd7d9 in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=6df951dcd7d9
description:
ruby: slicc: improve the grammar
This patch changes the grammar for SLICC so as to remove some of the
redundant / duplicate rules. In particular rules for object/variable
declaration and class member declaration have been unified. Similarly,
the
rules for a general function and a class method have been unified.
One more change is in the priority of two rules. The first rule is on
declaring a function with all the params typed and named. The second
rule is
on declaring a function with all the params only typed. Earlier the
second
rule had a higher priority. Now the first rule has a higher priority.
diffstat:
src/mem/slicc/ast/FuncDeclAST.py | 11 +-
src/mem/slicc/ast/ObjDeclAST.py | 28 +++++-
src/mem/slicc/ast/TypeFieldMemberAST.py | 57 -------------
src/mem/slicc/ast/TypeFieldMethodAST.py | 52 ------------
src/mem/slicc/ast/__init__.py | 2 -
src/mem/slicc/parser.py | 132 ++++++++++++++++---------------
src/mem/slicc/symbols/StateMachine.py | 1 +
src/mem/slicc/symbols/Type.py | 39 ++-------
8 files changed, 109 insertions(+), 213 deletions(-)
diffs (truncated from 478 to 300 lines):
diff -r 4c0de6e0669c -r 6df951dcd7d9 src/mem/slicc/ast/FuncDeclAST.py
--- a/src/mem/slicc/ast/FuncDeclAST.py Mon Sep 01 16:55:44 2014 -0500
+++ b/src/mem/slicc/ast/FuncDeclAST.py Mon Sep 01 16:55:44 2014 -0500
@@ -57,9 +57,14 @@
# Generate function header
for formal in self.formals:
# Lookup parameter types
- type, ident = formal.generate()
- types.append(type)
- params.append(ident)
+ try:
+ type, ident = formal.generate()
+ types.append(type)
+ params.append(ident)
+
+ except AttributeError:
+ types.append(formal.type)
+ params.append(None)
body = self.slicc.codeFormatter()
if self.statements is None:
diff -r 4c0de6e0669c -r 6df951dcd7d9 src/mem/slicc/ast/ObjDeclAST.py
--- a/src/mem/slicc/ast/ObjDeclAST.py Mon Sep 01 16:55:44 2014 -0500
+++ b/src/mem/slicc/ast/ObjDeclAST.py Mon Sep 01 16:55:44 2014 -0500
@@ -29,21 +29,23 @@
from slicc.symbols import Var
class ObjDeclAST(DeclAST):
- def __init__(self, slicc, type_ast, ident, pairs):
+ def __init__(self, slicc, type_ast, ident, pairs, rvalue):
super(ObjDeclAST, self).__init__(slicc, pairs)
self.type_ast = type_ast
self.ident = ident
+ self.rvalue = rvalue
def __repr__(self):
return "[ObjDecl: %r]" % self.ident
- def generate(self):
+ def generate(self, parent = None):
if "network" in self and not ("virtual_network" in self or
"physical_network" in self) :
self.error("Network queues require a 'virtual_network' attribute")
type = self.type_ast.type
+
if type.isBuffer and "ordered" not in self:
self.error("Buffer object decls require an 'ordered' attribute")
@@ -60,8 +62,6 @@
self.error("The 'random' attribute is '%s' " + \
"must be 'true' or 'false'.", value)
- machine = self.symtab.state_machine
-
# FIXME : should all use accessors here to avoid public member
# variables
if self.ident == "version":
@@ -73,10 +73,26 @@
else:
c_code = "(*m_%s_ptr)" % (self.ident)
+ # check type if this is a initialization
+ init_code = ""
+ if self.rvalue:
+ rvalue_type,init_code = self.rvalue.inline(True)
+ if type != rvalue_type:
+ self.error("Initialization type mismatch '%s' and '%s'" % \
+ (type, rvalue_type))
+
+ machine = self.symtab.state_machine
+
v = Var(self.symtab, self.ident, self.location, type, c_code,
self.pairs, machine)
- if machine:
+ # Add data member to the parent type
+ if parent:
+ if not parent.addDataMember(self.ident, type, self.pairs,
init_code):
+ self.error("Duplicate data member: %s:%s" % (parent,
self.ident))
+
+ elif machine:
machine.addObject(v)
- self.symtab.newSymbol(v)
+ else:
+ self.symtab.newSymbol(v)
diff -r 4c0de6e0669c -r 6df951dcd7d9 src/mem/slicc/ast/TypeFieldMemberAST.py
--- a/src/mem/slicc/ast/TypeFieldMemberAST.py Mon Sep 01 16:55:44 2014 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,57 +0,0 @@
-# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
-# Copyright (c) 2009 The Hewlett-Packard Development Company
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are
-# met: redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer;
-# redistributions in binary form must reproduce the above copyright
-# notice, this list of conditions and the following disclaimer in the
-# documentation and/or other materials provided with the distribution;
-# neither the name of the copyright holders nor the names of its
-# contributors may be used to endorse or promote products derived from
-# this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-from slicc.ast.TypeFieldAST import TypeFieldAST
-
-class TypeFieldMemberAST(TypeFieldAST):
- def __init__(self, slicc, type_ast, field_id, pairs, rvalue):
- super(TypeFieldMemberAST, self).__init__(slicc, pairs)
-
- self.type_ast = type_ast
- self.field_id = field_id
- self.rvalue = rvalue
-
- def __repr__(self):
- return "[TypeFieldMember: %r]" % self.field_id
-
- def generate(self, type):
- # Lookup type
- field_type = self.type_ast.type
-
- # check type if this is a initialization
- init_code = ""
- if self.rvalue:
- rvalue_type,init_code = self.rvalue.inline(True)
- if field_type != rvalue_type:
- self.error("Initialization type mismatch '%s' and '%s'" % \
- (field_type, rvalue_type))
-
- # Add data member to the parent type
- if not type.addDataMember(self.field_id, field_type, self.pairs,
- init_code):
-
- self.error("Duplicate data member: %s:%s" % (field_type,
self.field_id))
diff -r 4c0de6e0669c -r 6df951dcd7d9 src/mem/slicc/ast/TypeFieldMethodAST.py
--- a/src/mem/slicc/ast/TypeFieldMethodAST.py Mon Sep 01 16:55:44 2014 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,52 +0,0 @@
-# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
-# Copyright (c) 2009 The Hewlett-Packard Development Company
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are
-# met: redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer;
-# redistributions in binary form must reproduce the above copyright
-# notice, this list of conditions and the following disclaimer in the
-# documentation and/or other materials provided with the distribution;
-# neither the name of the copyright holders nor the names of its
-# contributors may be used to endorse or promote products derived from
-# this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-from slicc.ast.TypeFieldAST import TypeFieldAST
-
-class TypeFieldMethodAST(TypeFieldAST):
- def __init__(self, slicc, return_type_ast, ident, type_asts, pairs,
- statements = None):
- super(TypeFieldMethodAST, self).__init__(slicc, pairs)
-
- self.return_type_ast = return_type_ast
- self.ident = ident
- self.type_asts = type_asts
- self.statements = statements
-
- def __repr__(self):
- return ""
-
- def generate(self, type):
- # Lookup return type
- return_type = self.return_type_ast.type
-
- # Lookup parameter types
- types = [ t.type for t in self.type_asts ]
-
- # Add method
- if not type.addMethod(self.ident, return_type, types):
- self.error("Duplicate method: %s:%s()" % (type, self.ident))
diff -r 4c0de6e0669c -r 6df951dcd7d9 src/mem/slicc/ast/__init__.py
--- a/src/mem/slicc/ast/__init__.py Mon Sep 01 16:55:44 2014 -0500
+++ b/src/mem/slicc/ast/__init__.py Mon Sep 01 16:55:44 2014 -0500
@@ -67,7 +67,5 @@
from slicc.ast.TypeDeclAST import *
from slicc.ast.TypeFieldAST import *
from slicc.ast.TypeFieldEnumAST import *
-from slicc.ast.TypeFieldMemberAST import *
-from slicc.ast.TypeFieldMethodAST import *
from slicc.ast.TypeFieldStateAST import *
from slicc.ast.VarExprAST import *
diff -r 4c0de6e0669c -r 6df951dcd7d9 src/mem/slicc/parser.py
--- a/src/mem/slicc/parser.py Mon Sep 01 16:55:44 2014 -0500
+++ b/src/mem/slicc/parser.py Mon Sep 01 16:55:44 2014 -0500
@@ -318,9 +318,41 @@
p[4]["state_decl"] = "yes"
p[0] = ast.StateDeclAST(self, p[3], p[4], p[7])
- def p_decl__object(self, p):
- "decl : type ident pairs SEMI"
- p[0] = ast.ObjDeclAST(self, p[1], p[2], p[3])
+ # Type fields
+ def p_type_members__list(self, p):
+ "type_members : type_member type_members"
+ p[0] = [ p[1] ] + p[2]
+
+ def p_type_members__empty(self, p):
+ "type_members : empty"
+ p[0] = []
+
+ def p_type_member__0(self, p):
+ """type_member : obj_decl
+ | func_decl
+ | func_def"""
+ p[0] = p[1]
+
+ # Member / Variable declarations
+ def p_decl__obj_decl(self, p):
+ "decl : obj_decl"
+ p[0] = p[1]
+
+ def p_obj_decl__0(self, p):
+ "obj_decl : type ident pairs SEMI"
+ p[0] = ast.ObjDeclAST(self, p[1], p[2], p[3], None)
+
+ def p_obj_decl__1(self, p):
+ "obj_decl : type STAR ident pairs SEMI"
+ p[0] = ast.ObjDeclAST(self, p[1], p[3], p[4], None)
+
+ def p_obj_decl__2(self, p):
+ "obj_decl : type ident ASSIGN expr SEMI"
+ p[0] = ast.ObjDeclAST(self, p[1], p[2], ast.PairListAST(self), p[4])
+
+ def p_obj_decl__3(self, p):
+ "obj_decl : type STAR ident ASSIGN expr SEMI"
+ p[0] = ast.ObjDeclAST(self, p[1], p[3], ast.PairListAST(self), p[5])
# Function definition and declaration
def p_decl__func_decl(self, p):
@@ -332,6 +364,11 @@
| type ident '(' params ')' pairs SEMI"""
p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None)
+ def p_func_decl__1(self, p):
+ """func_decl : void ident '(' types ')' pairs SEMI
+ | type ident '(' types ')' pairs SEMI"""
+ p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None)
+
def p_decl__func_def(self, p):
"decl : func_def"
p[0] = p[1]
@@ -341,32 +378,6 @@
| type ident '(' params ')' pairs statements"""
p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7])
- # Type fields
- def p_type_members__list(self, p):
- "type_members : type_member type_members"
- p[0] = [ p[1] ] + p[2]
-
- def p_type_members__empty(self, p):
- "type_members : empty"
- p[0] = []
-
- def p_type_method__0(self, p):
- "type_member : type_or_void ident '(' types ')' pairs SEMI"
- p[0] = ast.TypeFieldMethodAST(self, p[1], p[2], p[4], p[6])
-
- def p_type_method__1(self, p):
- "type_member : type_or_void ident '(' params ')' pairs statements"
- p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7])
-
- def p_type_member__1(self, p):
- "type_member : type_or_void ident pairs SEMI"
- p[0] = ast.TypeFieldMemberAST(self, p[1], p[2], p[3], None)
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev