Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
L
latte
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zygzagZ
latte
Commits
0df5b04c
Commit
0df5b04c
authored
Dec 08, 2020
by
zygzagZ
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extend abstract ClassDef, add ParseError, WIP TypeCheck
parent
1f3906a4
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
290 additions
and
217 deletions
+290
-217
Absyn.h
Absyn.h
+11
-0
Info.cpp
Info.cpp
+0
-0
Info.h
Info.h
+30
-0
Latte.cpp
Latte.cpp
+8
-0
Makefile
Makefile
+6
-3
ParseError.h
ParseError.h
+12
-0
TypeCheck.cpp
TypeCheck.cpp
+119
-116
TypeCheck.h
TypeCheck.h
+104
-98
No files found.
Absyn.h
View file @
0df5b04c
...
...
@@ -258,6 +258,9 @@ public:
class
ClassDef
:
public
Visitable
{
public:
virtual
PIdent
*
getName
()
const
=
0
;
virtual
PIdent
*
getParent
()
const
=
0
;
virtual
ClassBlock
*
getBlock
()
const
=
0
;
virtual
ClassDef
*
clone
()
const
=
0
;
};
...
...
@@ -436,6 +439,10 @@ public:
virtual
void
accept
(
Visitor
*
v
);
virtual
ClassDefN
*
clone
()
const
;
void
swap
(
ClassDefN
&
);
virtual
PIdent
*
getName
()
const
{
return
pident_
;
};
virtual
PIdent
*
getParent
()
const
{
return
NULL
;
};
virtual
ClassBlock
*
getBlock
()
const
{
return
classblock_
;
};
};
class
ClassDefE
:
public
ClassDef
...
...
@@ -452,6 +459,10 @@ public:
virtual
void
accept
(
Visitor
*
v
);
virtual
ClassDefE
*
clone
()
const
;
void
swap
(
ClassDefE
&
);
virtual
PIdent
*
getName
()
const
{
return
pident_1
;
};
virtual
PIdent
*
getParent
()
const
{
return
pident_2
;
};
virtual
ClassBlock
*
getBlock
()
const
{
return
classblock_
;
};
};
class
ClassBl
:
public
ClassBlock
...
...
Info.cpp
0 → 100644
View file @
0df5b04c
Info.h
0 → 100644
View file @
0df5b04c
#ifndef INFO_HEADER
#define INFO_HEADER
#include "Absyn.h"
using
namespace
std
;
class
VarInfo
{
string
name
;
string
type
;
bool
operator
<
(
const
VarInfo
&
other
)
const
{
return
name
!=
other
.
name
?
name
<
other
.
name
:
type
<
other
.
type
;
}
}
class
FunctionInfo
:
VarInfo
{
vector
<
VarInfo
>
arguments
;
};
class
ClassInfo
:
VarInfo
{
ClassInfo
*
parent
;
public:
ClassInfo
*
getParent
()
const
{
return
parent
;
};
vector
<
FunctionInfo
>
functions
;
vector
<
VarInfo
>
variables
;
};
vector
<
ClassInfo
>
classes
;
vector
<
FunctionInfo
>
functions
;
Latte.cpp
View file @
0df5b04c
...
...
@@ -4,6 +4,8 @@
#include "Printer.h"
#include "Absyn.h"
// #include "Compiler.h"
#include "ParseError.h"
#include "TypeCheck.h"
#include <fstream>
#include <iostream>
#include <filesystem>
...
...
@@ -63,6 +65,12 @@ int main(int argc, char ** argv)
binaryPath
=
argv
[
0
];
std
::
filesystem
::
path
file
(
filename
?
filename
:
"Instant"
);
TypeCheck
checker
;
try
{
checker
.
check
(
parse_tree
);
}
catch
(
ParseError
&
e
)
{
std
::
cerr
<<
e
.
what
()
<<
std
::
endl
;
}
/*Compiler c(file);
std::string out = c.compile(parse_tree);
...
...
Makefile
View file @
0df5b04c
CC
=
g++
CCFLAGS
=
-g
-W
-Wall
-O3
-std
=
c++1z
CCFLAGS
=
-g
-W
-Wall
-O3
-std
=
c++1z
-Wno-unused-parameter
FLEX
=
flex
FLEX_OPTS
=
-PGrammar
...
...
@@ -7,7 +7,7 @@ FLEX_OPTS=-PGrammar
BISON
=
bison
BISON_OPTS
=
-t
-pGrammar
OBJS
=
Absyn.o Lexer.o Parser.o Printer.o
OBJS
=
Absyn.o Lexer.o Parser.o Printer.o
TypeCheck.o
.PHONY
:
clean distclean
...
...
@@ -44,5 +44,8 @@ Printer.o : Printer.cpp Printer.h Absyn.h
Skeleton.o
:
Skeleton.cpp Skeleton.h Absyn.h
${
CC
}
${
CCFLAGS
}
-Wno-unused-parameter
-c
Skeleton.cpp
Latte.o
:
Latte.cpp Parser.h Printer.h Absyn.h
Latte.o
:
Latte.cpp Parser.h Printer.h Absyn.h
ParseError.h
${
CC
}
${
CCFLAGS
}
-c
Latte.cpp
TypeCheck.o
:
TypeCheck.cpp TypeCheck.h Absyn.h ParseError.h
${
CC
}
${
CCFLAGS
}
-c
TypeCheck.cpp
ParseError.h
0 → 100644
View file @
0df5b04c
#ifndef ERROR_HEADER
#define ERROR_HEADER
#include <string>
using
namespace
std
;
class
ParseError
:
std
::
exception
{
string
msg
;
public:
ParseError
(
string
reason
)
:
msg
(
reason
)
{}
const
char
*
what
()
{
return
msg
.
data
();
}
};
#endif
\ No newline at end of file
TypeCheck.cpp
View file @
0df5b04c
This diff is collapsed.
Click to expand it.
TypeCheck.h
View file @
0df5b04c
...
...
@@ -5,107 +5,113 @@
#include "Absyn.h"
class
Skeleton
:
public
Visitor
class
TypeCheck
:
public
Visitor
{
enum
State
{
initialized
,
buildInfo
,
checkType
}
state
;
public:
void
visitProgram
(
Program
*
p
);
void
visitTopDef
(
TopDef
*
p
);
void
visitFunDef
(
FunDef
*
p
);
void
visitArg
(
Arg
*
p
);
void
visitClassDef
(
ClassDef
*
p
);
void
visitClassBlock
(
ClassBlock
*
p
);
void
visitClassBlockDef
(
ClassBlockDef
*
p
);
void
visitBlock
(
Block
*
p
);
void
visitStmt
(
Stmt
*
p
);
void
visitItem
(
Item
*
p
);
void
visitType
(
Type
*
p
);
void
visitExpr
(
Expr
*
p
);
void
visitAddOp
(
AddOp
*
p
);
void
visitMulOp
(
MulOp
*
p
);
void
visitRelOp
(
RelOp
*
p
);
void
visitProg
(
Prog
*
p
);
void
visitFnDef
(
FnDef
*
p
);
void
visitClDef
(
ClDef
*
p
);
void
visitFuncDef
(
FuncDef
*
p
);
void
visitAr
(
Ar
*
p
);
void
visitClassDefN
(
ClassDefN
*
p
);
void
visitClassDefE
(
ClassDefE
*
p
);
void
visitClassBl
(
ClassBl
*
p
);
void
visitClassMthd
(
ClassMthd
*
p
);
void
visitClassFld
(
ClassFld
*
p
);
void
visitBlk
(
Blk
*
p
);
void
visitEmpty
(
Empty
*
p
);
void
visitBStmt
(
BStmt
*
p
);
void
visitDecl
(
Decl
*
p
);
void
visitNoInit
(
NoInit
*
p
);
void
visitInit
(
Init
*
p
);
void
visitAss
(
Ass
*
p
);
void
visitTableAss
(
TableAss
*
p
);
void
visitTableIncr
(
TableIncr
*
p
);
void
visitTableDecr
(
TableDecr
*
p
);
void
visitIncr
(
Incr
*
p
);
void
visitDecr
(
Decr
*
p
);
void
visitRet
(
Ret
*
p
);
void
visitVRet
(
VRet
*
p
);
void
visitCond
(
Cond
*
p
);
void
visitCondElse
(
CondElse
*
p
);
void
visitWhile
(
While
*
p
);
void
visitSExp
(
SExp
*
p
);
void
visitForEach
(
ForEach
*
p
);
void
visitInt
(
Int
*
p
);
void
visitStr
(
Str
*
p
);
void
visitBool
(
Bool
*
p
);
void
visitVoid
(
Void
*
p
);
void
visitArray
(
Array
*
p
);
void
visitClassT
(
ClassT
*
p
);
void
visitFun
(
Fun
*
p
);
void
visitEVar
(
EVar
*
p
);
void
visitELitInt
(
ELitInt
*
p
);
void
visitELitTrue
(
ELitTrue
*
p
);
void
visitELitFalse
(
ELitFalse
*
p
);
void
visitEApp
(
EApp
*
p
);
void
visitEString
(
EString
*
p
);
void
visitENewArray
(
ENewArray
*
p
);
void
visitENewClass
(
ENewClass
*
p
);
void
visitEClsMmbr
(
EClsMmbr
*
p
);
void
visitEClsMthd
(
EClsMthd
*
p
);
void
visitNull
(
Null
*
p
);
void
visitEIndexAcc
(
EIndexAcc
*
p
);
void
visitECast
(
ECast
*
p
);
void
visitNeg
(
Neg
*
p
);
void
visitNot
(
Not
*
p
);
void
visitEMul
(
EMul
*
p
);
void
visitEAdd
(
EAdd
*
p
);
void
visitERel
(
ERel
*
p
);
void
visitEAnd
(
EAnd
*
p
);
void
visitEOr
(
EOr
*
p
);
void
visitPlus
(
Plus
*
p
);
void
visitMinus
(
Minus
*
p
);
void
visitTimes
(
Times
*
p
);
void
visitDiv
(
Div
*
p
);
void
visitMod
(
Mod
*
p
);
void
visitLTH
(
LTH
*
p
);
void
visitLE
(
LE
*
p
);
void
visitGTH
(
GTH
*
p
);
void
visitGE
(
GE
*
p
);
void
visitEQU
(
EQU
*
p
);
void
visitNE
(
NE
*
p
);
void
visitListTopDef
(
ListTopDef
*
p
);
void
visitListArg
(
ListArg
*
p
);
void
visitListClassBlockDef
(
ListClassBlockDef
*
p
);
void
visitListStmt
(
ListStmt
*
p
);
void
visitListItem
(
ListItem
*
p
);
void
visitListType
(
ListType
*
p
);
void
visitListExpr
(
ListExpr
*
p
);
void
visitPIdent
(
PIdent
*
p
);
void
visitInteger
(
Integer
x
);
void
visitChar
(
Char
x
);
void
visitDouble
(
Double
x
);
void
visitString
(
String
x
);
void
visitIdent
(
Ident
x
);
TypeCheck
()
:
state
(
initialized
)
{};
void
check
(
Visitable
*
v
);
protected:
void
visitProgram
(
Program
*
p
);
void
visitTopDef
(
TopDef
*
p
);
void
visitFunDef
(
FunDef
*
p
);
void
visitArg
(
Arg
*
p
);
void
visitClassDef
(
ClassDef
*
p
);
void
visitClassBlock
(
ClassBlock
*
p
);
void
visitClassBlockDef
(
ClassBlockDef
*
p
);
void
visitBlock
(
Block
*
p
);
void
visitStmt
(
Stmt
*
p
);
void
visitItem
(
Item
*
p
);
void
visitType
(
Type
*
p
);
void
visitExpr
(
Expr
*
p
);
void
visitAddOp
(
AddOp
*
p
);
void
visitMulOp
(
MulOp
*
p
);
void
visitRelOp
(
RelOp
*
p
);
void
visitProg
(
Prog
*
p
);
void
visitFnDef
(
FnDef
*
p
);
void
visitClDef
(
ClDef
*
p
);
void
visitFuncDef
(
FuncDef
*
p
);
void
visitAr
(
Ar
*
p
);
void
visitClassDefN
(
ClassDefN
*
p
);
void
visitClassDefE
(
ClassDefE
*
p
);
void
visitClassBl
(
ClassBl
*
p
);
void
visitClassMthd
(
ClassMthd
*
p
);
void
visitClassFld
(
ClassFld
*
p
);
void
visitBlk
(
Blk
*
p
);
void
visitEmpty
(
Empty
*
p
);
void
visitBStmt
(
BStmt
*
p
);
void
visitDecl
(
Decl
*
p
);
void
visitNoInit
(
NoInit
*
p
);
void
visitInit
(
Init
*
p
);
void
visitAss
(
Ass
*
p
);
void
visitTableAss
(
TableAss
*
p
);
void
visitTableIncr
(
TableIncr
*
p
);
void
visitTableDecr
(
TableDecr
*
p
);
void
visitIncr
(
Incr
*
p
);
void
visitDecr
(
Decr
*
p
);
void
visitRet
(
Ret
*
p
);
void
visitVRet
(
VRet
*
p
);
void
visitCond
(
Cond
*
p
);
void
visitCondElse
(
CondElse
*
p
);
void
visitWhile
(
While
*
p
);
void
visitSExp
(
SExp
*
p
);
void
visitForEach
(
ForEach
*
p
);
void
visitInt
(
Int
*
p
);
void
visitStr
(
Str
*
p
);
void
visitBool
(
Bool
*
p
);
void
visitVoid
(
Void
*
p
);
void
visitArray
(
Array
*
p
);
void
visitClassT
(
ClassT
*
p
);
void
visitFun
(
Fun
*
p
);
void
visitEVar
(
EVar
*
p
);
void
visitELitInt
(
ELitInt
*
p
);
void
visitELitTrue
(
ELitTrue
*
p
);
void
visitELitFalse
(
ELitFalse
*
p
);
void
visitEApp
(
EApp
*
p
);
void
visitEString
(
EString
*
p
);
void
visitENewArray
(
ENewArray
*
p
);
void
visitENewClass
(
ENewClass
*
p
);
void
visitEClsMmbr
(
EClsMmbr
*
p
);
void
visitEClsMthd
(
EClsMthd
*
p
);
void
visitNull
(
Null
*
p
);
void
visitEIndexAcc
(
EIndexAcc
*
p
);
void
visitECast
(
ECast
*
p
);
void
visitNeg
(
Neg
*
p
);
void
visitNot
(
Not
*
p
);
void
visitEMul
(
EMul
*
p
);
void
visitEAdd
(
EAdd
*
p
);
void
visitERel
(
ERel
*
p
);
void
visitEAnd
(
EAnd
*
p
);
void
visitEOr
(
EOr
*
p
);
void
visitPlus
(
Plus
*
p
);
void
visitMinus
(
Minus
*
p
);
void
visitTimes
(
Times
*
p
);
void
visitDiv
(
Div
*
p
);
void
visitMod
(
Mod
*
p
);
void
visitLTH
(
LTH
*
p
);
void
visitLE
(
LE
*
p
);
void
visitGTH
(
GTH
*
p
);
void
visitGE
(
GE
*
p
);
void
visitEQU
(
EQU
*
p
);
void
visitNE
(
NE
*
p
);
void
visitListTopDef
(
ListTopDef
*
p
);
void
visitListArg
(
ListArg
*
p
);
void
visitListClassBlockDef
(
ListClassBlockDef
*
p
);
void
visitListStmt
(
ListStmt
*
p
);
void
visitListItem
(
ListItem
*
p
);
void
visitListType
(
ListType
*
p
);
void
visitListExpr
(
ListExpr
*
p
);
void
visitPIdent
(
PIdent
*
p
);
void
visitInteger
(
Integer
x
);
void
visitChar
(
Char
x
);
void
visitDouble
(
Double
x
);
void
visitString
(
String
x
);
void
visitIdent
(
Ident
x
);
};
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment