Skip to content
This repository has been archived by the owner on Jul 30, 2021. It is now read-only.

Commit

Permalink
its comming.
Browse files Browse the repository at this point in the history
  • Loading branch information
uanhi committed Mar 29, 2021
1 parent 1bbc691 commit 82d862b
Show file tree
Hide file tree
Showing 25 changed files with 526 additions and 345 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SRC = $(wildcard ${DIR_SRC}/*.cc)
TMP = $(wildcard $(DIR_TMP)/*.o)

all:
${CC} -std=c++20 -c $(foreach i, $(SRC), $(i))
${CC} -std=c++20 -c -Os $(foreach i, $(SRC), $(i))

install:
$(shell mkdir ${DIR_TMP})
Expand Down
189 changes: 36 additions & 153 deletions src/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,136 +18,10 @@
#include <vector>

#include "token.h"
#include "type.h"

// abstract syntax tree
namespace ast {
// types for drift
enum TypeKind {
T_INT, // int
T_FLOAT, // float
T_STR, // str
T_CHAR, // char
T_BOOL, // bool
T_ARRAY, // []<T>
T_MAP, // <T1, T2>
T_TUPLE, // (T)
T_USER, // user
};

// basic type for drift
//
#define S_INT "int" // 1
#define S_FLOAT "float" // 2
#define S_STR "str" // 3
#define S_CHAR "char" // 4
#define S_BOOL "bool" // 5

// TYPE
class Type {
public:
// stringer
virtual std::string stringer() = 0;
// kind of basic type
virtual TypeKind kind() = 0;
};

// <int>
class Int : public Type {
public:
std::string stringer() override { return "<Int>"; }

TypeKind kind() override { return T_INT; }
};

// float
class Float : public Type {
public:
std::string stringer() override { return "<Float>"; }

TypeKind kind() override { return T_FLOAT; }
};

// str
class Str : public Type {
public:
std::string stringer() override { return "<Str>"; }

TypeKind kind() override { return T_STR; }
};

// char
class Char : public Type {
public:
std::string stringer() override { return "<Char>"; }

TypeKind kind() override { return T_CHAR; }
};

// bool
class Bool : public Type {
public:
std::string stringer() override { return "<Bool>"; }

TypeKind kind() override { return T_BOOL; }
};

// array (not keyword, for compiler analysis)
// []<type>
class Array : public Type {
public:
Type *T; // type for elements

explicit Array(Type *T) : T(T) {}

std::string stringer() override { return "<Array T=" + T->stringer() + " >"; }

TypeKind kind() override { return T_ARRAY; }
};

// map (not keyword, for compiler analysis)
// <type, type>
class Map : public Type {
public:
Type *T1; // K
Type *T2; // V

explicit Map(Type *T1, Type *T2) : T1(T1), T2(T2) {}

std::string stringer() override {
return "<Map T1=" + T1->stringer() + " T2=" + T2->stringer() + " >";
}

TypeKind kind() override { return T_MAP; }
};

// tuple (not keyword, for compiler analysis)
// (type)
class Tuple : public Type {
public:
Type *T; // type for elements

explicit Tuple(Type *T) : T(T) {}

std::string stringer() override { return "<Tuple T=" + T->stringer() + " >"; }

TypeKind kind() override { return T_TUPLE; }
};

// user definition type
// `type`
class User : public Type {
public:
token::Token name;

explicit User(token::Token name) { this->name = std::move(name); }

std::string stringer() override {
return "<User Name='" + name.literal + "' >";
}

TypeKind kind() override { return T_USER; }
};

// ast types
enum Kind {
// expression
Expand All @@ -166,23 +40,24 @@ enum Kind {
EXPR_INDEX, // EXPR[EXPR]
EXPR_NEW,
// statement
STMT_EXPR, // EXPR
STMT_VAR, // VAR
STMT_BLOCK, // BLOCK
STMT_IF, // IF
STMT_FOR, // FOR
STMT_DO, // DO
STMT_OUT, // OUT
STMT_TIN, // TIN
STMT_FUNC, // FUNC
STMT_WHOLE, // CLASS | ENUM
STMT_AND, // AND
STMT_MOD, // MOD
STMT_USE, // USE
STMT_RET, // RET
STMT_ENUM, // ENUM
STMT_INHERIT, // <- <name> + <name>..
STMT_INTERFACE // INTERFACE
STMT_EXPR, // EXPR
STMT_VAR, // VAR
STMT_BLOCK, // BLOCK
STMT_IF, // IF
STMT_FOR, // FOR
STMT_DO, // DO
STMT_OUT, // OUT
STMT_TIN, // TIN
STMT_FUNC, // FUNC
STMT_WHOLE, // CLASS | ENUM
STMT_AND, // AND
STMT_MOD, // MOD
STMT_USE, // USE
STMT_RET, // RET
STMT_ENUM, // ENUM
STMT_INHERIT, // <- <name> + <name>..
STMT_INTERFACE, // INTERFACE
STMT_DEL // DEL
};

// K1: V1 | K1 + K2: V2
Expand Down Expand Up @@ -528,26 +403,20 @@ class VarStmt : public Stmt {
// default is not init
Expr *expr = nullptr;

bool local = false;

// has expr
explicit VarStmt(token::Token name, Type *T, Expr *e, bool local)
: T(T), expr(e) {
explicit VarStmt(token::Token name, Type *T, Expr *e) : T(T), expr(e) {
this->name = std::move(name);
this->local = local;
}

// not init expr
explicit VarStmt(token::Token name, Type *T, bool local) : T(T) {
explicit VarStmt(token::Token name, Type *T) : T(T) {
this->name = std::move(name);
this->local = local;
}

std::string stringer() override {
std::stringstream str;

str << "<VarStmt { Name='" << name.literal << "' Type=" << T->stringer()
<< " Local=" << (local ? "T" : "F");
str << "<VarStmt { Name='" << name.literal << "' Type=" << T->stringer();

if (expr != nullptr)
str << " Expr=" << expr->stringer() << " }>";
Expand Down Expand Up @@ -970,6 +839,20 @@ class EnumStmt : public Stmt {

Kind kind() override { return STMT_ENUM; }
};

// del
class DelStmt : public Stmt {
public:
token::Token name;

explicit DelStmt(token::Token n) : name(std::move(n)) {}

std::string stringer() override {
return "<DelStmt { Name='" + name.literal + "' }>";
}

Kind kind() override { return STMT_DEL; }
};
}; // namespace ast

#endif
9 changes: 7 additions & 2 deletions src/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void Compiler::compile() {

while (this->position < this->statements.size()) {
this->line = this->lineno.at(this->position);

this->stmt(look()); // START
this->position++;
}
Expand Down Expand Up @@ -61,7 +61,7 @@ void Compiler::emitName(std::string v) {
}

// push names type to entity
void Compiler::emitType(ast::Type *t) {
void Compiler::emitType(Type *t) {
this->now->types.push_back(t);
this->emitOffset(this->itf++);
}
Expand Down Expand Up @@ -607,6 +607,11 @@ void Compiler::stmt(ast::Stmt *stmt) {
this->emitConstant(obj); // push to constant object
} break;
//
case ast::STMT_DEL: {
this->emitCode(byte::DEL);
this->emitName(static_cast<ast::DelStmt *>(stmt)->name.literal); // name
} break;
//
default:
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <algorithm>

#include "ast.h"
#include "type.h"
#include "entity.h"
#include "object.h"
#include "opcode.h"
Expand All @@ -43,7 +44,7 @@ class Compiler {
void emitOffset(int); // push offset to entity
void emitConstant(object::Object *); // push constant to entity
void emitName(std::string); // push name to entity
void emitType(ast::Type *); // push names type to entity
void emitType(Type *); // push names type to entity

void emitJumpOffset(int);

Expand Down
4 changes: 3 additions & 1 deletion src/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <vector>

#include "ast.h"
#include "type.h"
#include "object.h"
#include "opcode.h"

Expand All @@ -33,7 +34,7 @@ struct Entity {
std::vector<int> offsets; // offset of bytecode
std::vector<object::Object *> constants; // constant
std::vector<std::string> names; // names
std::vector<ast::Type *> types; // type of variables
std::vector<Type *> types; // type of variables

std::vector<int> lineno; // line no of each bytecode

Expand Down Expand Up @@ -89,6 +90,7 @@ struct Entity {
case byte::GET:
case byte::SET:
case byte::MOD:
case byte::DEL:
case byte::USE: {
printf("%10d %5d: %s %12d '%s'\n", ip, lineno.at(ip),
byte::codeString[codes.at(ip)].c_str(), offsets.at(op++),
Expand Down
1 change: 0 additions & 1 deletion src/lexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ void Lexer::lexSymbol() {
case '_':
tok.kind = token::UNDERLINE;
break;
break;

default:
error(exp::UNKNOWN_SYMBOL, "unknown symbol: " + std::to_string(now()));
Expand Down
5 changes: 3 additions & 2 deletions src/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define DRIFT_OBJECT_H

#include "ast.h"
#include "type.h"

struct Entity;
struct Frame;
Expand Down Expand Up @@ -276,7 +277,7 @@ class Func : public Object {
std::string name; // function name

ast::FuncArg arguments; // function args
ast::Type *ret; // function return
Type *ret; // function return

Entity *entity; // function entity

Expand All @@ -296,7 +297,7 @@ class Whole : public Object {
Entity *entity; // whole entity

// interface definition
std::vector<std::tuple<std::string, ast::FaceArg, ast::Type *>> interface;
std::vector<std::tuple<std::string, ast::FaceArg, Type *>> interface;
// inherit definition
std::vector<std::string> inherit;

Expand Down
21 changes: 12 additions & 9 deletions src/opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// bytecode
namespace byte {
// total number of bytecodes
constexpr int len = 40;
constexpr int len = 41;
// bytecode type
enum Code {
CONST, // CONST
Expand All @@ -35,9 +35,11 @@ enum Code {
ORIG, // ORIG
NAME, // NAME
NEW, // NEW
FUNC, // FUNC
WHOLE, // WHOLE
ENUM, // ENUM
DEL, // DEL

FUNC, // FUNC
WHOLE, // WHOLE
ENUM, // ENUM

MOD, // MOD
USE, // USE
Expand Down Expand Up @@ -75,11 +77,12 @@ enum Code {

// return a string of bytecode
static std::string codeString[len] = {
"CONST", "ASSIGN", "STORE", "LOAD", "INDEX", "REPLACE", "GET", "SET",
"CALL", "ORIG", "NAME", "NEW", "FUNC", "WHOLE", "ENUM", "MOD",
"USE", "B_ARR", "B_TUP", "B_MAP", "ADD", "SUB", "MUL", "DIV",
"SUR", "GR", "LE", "GR_E", "LE_E", "E_E", "N_E", "AND",
"OR", "BANG", "NOT", "JUMP", "F_JUMP", "T_JUMP", "RET_N", "RET",
"CONST", "ASSIGN", "STORE", "LOAD", "INDEX", "REPLACE", "GET",
"SET", "CALL", "ORIG", "NAME", "NEW", "DEL", "FUNC",
"WHOLE", "ENUM", "MOD", "USE", "B_ARR", "B_TUP", "B_MAP",
"ADD", "SUB", "MUL", "DIV", "SUR", "GR", "LE",
"GR_E", "LE_E", "E_E", "N_E", "AND", "OR", "BANG",
"NOT", "JUMP", "F_JUMP", "T_JUMP", "RET_N", "RET",
};
}; // namespace byte

Expand Down
Loading

0 comments on commit 82d862b

Please sign in to comment.