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
3bc2b0ce
Commit
3bc2b0ce
authored
Dec 31, 2020
by
zygzagZ
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WIP compiler
parent
be5be9c9
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
243 additions
and
27 deletions
+243
-27
Makefile
Makefile
+6
-2
hello32.s
hello32.s
+10
-2
Compiler.cpp
src/Compiler.cpp
+24
-0
Compiler.h
src/Compiler.h
+183
-0
Latte.cpp
src/Latte.cpp
+20
-22
TypeCheck.h
src/TypeCheck.h
+0
-1
No files found.
Makefile
View file @
3bc2b0ce
CC
=
g++
CCFLAGS
=
-g
-W
-Wall
-O0
-std
=
c++2a
-Wno-unused-parameter
OBJS
=
Absyn.o Lexer.o Parser.o Printer.o TypeCheck.o Info.o ParseError.o
OBJS
=
Absyn.o Lexer.o Parser.o Printer.o TypeCheck.o Info.o ParseError.o
Compiler.o
.PHONY
:
clean distclean
...
...
@@ -35,12 +35,16 @@ Latte.o : src/Latte.cpp src/Parser.h src/Printer.h src/Absyn.h src/ParseError.h
TypeCheck.o
:
src/TypeCheck.cpp src/TypeCheck.h src/Info.h src/InfoList.h src/Absyn.h src/ParseError.h
${
CC
}
${
CCFLAGS
}
-c
src/TypeCheck.cpp
Compiler.o
:
src/Compiler.cpp src/Compiler.h src/TypeCheck.h src/Info.h src/InfoList.h src/Absyn.h src/ParseError.h
${
CC
}
${
CCFLAGS
}
-c
src/Compiler.cpp
Info.o
:
src/Info.cpp src/Info.h src/InfoList.h src/Absyn.h
${
CC
}
${
CCFLAGS
}
-c
src/Info.cpp
ParseError.o
:
src/ParseError.cpp src/Info.h src/InfoList.h src/Absyn.h src/ParseError.h
${
CC
}
${
CCFLAGS
}
-c
src/ParseError.cpp
lib/runtime.o
:
lib/runtime.c Makefile
gcc
-m32
-static
-L
/usr/lib/gcc-cross/i686-linux-gnu/8/
-c
lib/runtime.c
-o
lib/runtime.o
-fno-stack-protector
...
...
@@ -48,4 +52,4 @@ hello32.o : hello32.s Makefile
as
--32
hello32.s
-o
hello32.o
hello
:
hello32.o lib/runtime.o Makefile
ld
-m
elf_i386
-Llib
/
-e
main
lib/runtime.o hello32.o
-lc
-o
hello
ld
-m
elf_i386
-Llib
/ lib/runtime.o hello32.o
-lc
-o
hello
hello32.s
View file @
3bc2b0ce
...
...
@@ -7,6 +7,14 @@ hello:
.globl readStr
.globl error
.globl exit
.globl _start
_start:
pushl %ebp
movl %esp, %ebp
call main
pushl %eax
call exit
main:
pushl %ebp
...
...
@@ -14,5 +22,5 @@ movl %esp, %ebp
pushl $hello
call printStr
movl $0, %eax
pushl $0
call error
leave
ret
src/Compiler.cpp
0 → 100644
View file @
3bc2b0ce
#include "Compiler.h"
#include <filesystem>
extern
std
::
filesystem
::
path
binaryPath
;
const
std
::
string
Compiler
::
extension
=
"s"
;
void
Compiler
::
externalCommand
(
std
::
filesystem
::
path
lat
)
{
auto
obj
=
lat
;
obj
.
replace_extension
(
"o"
);
auto
cmd
=
std
::
string
(
"as --32 '"
)
+
lat
.
string
()
+
"' -o '"
+
obj
.
string
()
+
"'"
;
cerr
<<
cmd
<<
endl
;
system
(
cmd
.
data
());
auto
lib
=
binaryPath
.
parent_path
()
/
"lib"
;
auto
runtime
=
lib
/
"runtime.o"
;
auto
result
=
obj
;
result
.
replace_extension
(
""
);
cmd
=
std
::
string
(
"ld -m elf_i386 -L'"
)
+
lib
.
string
()
+
"' '"
+
runtime
.
string
()
+
"' '"
+
obj
.
string
()
+
"' -o '"
+
result
.
string
()
+
"'"
;
cerr
<<
cmd
<<
endl
;
system
(
cmd
.
data
());
}
std
::
string
Compiler
::
compile
(
Visitable
*
v
)
{
check
(
v
);
return
"empty
\n
"
;
}
\ No newline at end of file
src/Compiler.h
0 → 100644
View file @
3bc2b0ce
#ifndef COMPILER_HEADER
#define COMPILER_HEADER
/* You might want to change the above name. */
#include "TypeCheck.h"
#include <filesystem>
class
Compiler
:
public
TypeCheck
{
public:
Compiler
(
std
::
string
f
)
:
file
(
f
)
{};
std
::
string
compile
(
Visitable
*
v
);
static
const
std
::
string
extension
;
static
void
externalCommand
(
std
::
filesystem
::
path
file
);
private:
std
::
filesystem
::
path
file
;
// enum State {
// initialized,
// findClasses,
// findFunctions,
// visit
// } state;
// shared_ptr<Scope> scope;
// shared_ptr<Type> lastType, returnType;
// set<int> posv;
// vector<pair<ClassInfoPtr, ClassDef*>> classDefs;
// void checkFunction(FunctionInfoPtr f);
// void checkReturnStatement(shared_ptr<Type> type, Stmt *stmt);
// void checkFunctionRet();
// void addIOFunctions(Type &type);
// void setupEnv();
// template<typename T>
// shared_ptr<T> evalExpr(Visitable *expr) {
// if (!expr) throw runtime_error("No expr to eval");
// lastType = nullptr;
// try {
// expr->accept(this);
// } catch (const ParseError &err) {
// throw ParseError(err, expr);
// }
// shared_ptr<T> ret = dynamic_pointer_cast<T>(lastType);
// if (!ret) {
// T expect;
// throw InvalidTypeError(expect, {lastType}, expr);
// }
// lastType = nullptr;
// return ret;
// };
// shared_ptr<Type> evalExpr(Visitable *expr) {
// if (!expr) throw runtime_error("No expr to eval");
// try {
// expr->accept(this);
// } catch (const ParseError &err) {
// throw ParseError(err, expr);
// }
// if (!lastType) throw ParseError("No expression type", expr);
// auto ret = lastType;
// lastType = nullptr;
// return ret;
// };
// public:
// BindingPtr getParentBinding() const { return scope->currentClass ? static_pointer_cast<Binding>(scope->currentClass) : static_pointer_cast<Binding>(scope); }
// TypeCheck();
// void check(Visitable *v);
// protected:
// void visitClassDef(ClassDef *p);
// void visitProg(Prog *p);
// void visitFnDef(FnDef *p);
// void visitClDef(ClDef *p);
// void visitFuncDef(FuncDef *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 visitBStmt(BStmt *p);
// void visitDecl(Decl *p);
// void visitAss(Ass *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 visitEIndexAcc(EIndexAcc *p);
// void visitEClsMmbr(EClsMmbr *p);
// void visitEApp(EApp *p);
// void visitELitInt(ELitInt *p);
// void visitELitTrue(ELitTrue *p);
// void visitELitFalse(ELitFalse *p);
// void visitEString(EString *p);
// void visitENewArray(ENewArray *p);
// void visitENewClass(ENewClass *p);
// void visitNullCast(NullCast *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 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 visitString(String x);
// // abstract
// // topdefs
// void visitProgram(Program *t) {} //abstract class
// void visitTopDef(TopDef *t) {} //abstract class
// void visitFunDef(FunDef *t) {} //abstract class
// void visitBlock(Block *t) {};
// void visitClassBlock(ClassBlock *p) {};
// void visitClassBlockDef(ClassBlockDef *p) {};
// void visitItem(Item *p) {};
// void visitArg(Arg *p) {}
// void visitStmt(Stmt *p) {};
// void visitType(Type *p) {};
// void visitExpr(Expr *p) {};
// void visitAddOp(AddOp *p) {};
// void visitMulOp(MulOp *p) {};
// void visitRelOp(RelOp *p) {};
// // stmts
// void visitEmpty(Empty *p) {};
// void visitNoInit(NoInit *p) { };
// void visitInit(Init *p) { };
// void visitAr(Ar *p) {
// p->type_->accept(this);
// if (dynamic_cast<Void*>(p->type_)) {
// throw ParseError("Void argument", p->type_);
// }
// };
// // exprs
// 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) {};
// // terminals
// void visitDouble(Double x) {};
// void visitIdent(Ident x) {};
};
#endif
src/Latte.cpp
View file @
3bc2b0ce
...
...
@@ -3,7 +3,7 @@
#include "Parser.h"
#include "Printer.h"
#include "Absyn.h"
//
#include "Compiler.h"
#include "Compiler.h"
#include "ParseError.h"
#include "TypeCheck.h"
#include <fstream>
...
...
@@ -75,8 +75,25 @@ int main(int argc, char ** argv)
binaryPath
=
argv
[
0
];
std
::
filesystem
::
path
file
(
filename
?
filename
:
"Latte"
);
try
{
TypeCheck
checker
;
checker
.
check
(
parse_tree
);
Compiler
c
(
file
);
std
::
string
out
=
c
.
compile
(
parse_tree
);
if
(
!
filename
)
{
printf
(
"%s
\n
"
,
out
.
data
());
return
0
;
}
file
.
replace_extension
(
Compiler
::
extension
);
std
::
ofstream
ll
(
file
);
if
(
!
ll
)
{
fprintf
(
stderr
,
"Unable to open %s
\n
"
,
file
.
c_str
());
return
2
;
}
ll
<<
out
;
ll
.
close
();
Compiler
::
externalCommand
(
file
);
}
catch
(
ParseError
const
&
e
)
{
std
::
cerr
<<
"ERROR"
<<
endl
;
std
::
cerr
<<
e
.
what
()
<<
std
::
endl
;
...
...
@@ -102,24 +119,5 @@ int main(int argc, char ** argv)
return
1
;
}
std
::
cerr
<<
"OK"
<<
endl
;
/*Compiler c(file);
std::string out = c.compile(parse_tree);
if (!filename) {
printf("%s\n", out.data());
return 0;
}
file.replace_extension(Compiler::extension);
std::ofstream ll(file);
if (!ll) {
fprintf(stderr, "Unable to open %s\n", file.c_str());
return 2;
}
ll << out;
ll.close();
Compiler::externalCommand(file);*/
}
src/TypeCheck.h
View file @
3bc2b0ce
...
...
@@ -19,7 +19,6 @@ class TypeCheck : public Visitor
shared_ptr
<
Scope
>
scope
;
shared_ptr
<
Type
>
lastType
,
returnType
;
set
<
int
>
posv
;
vector
<
pair
<
ClassInfoPtr
,
ClassDef
*>>
classDefs
;
...
...
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