restructured headers
This commit is contained in:
parent
6c32f09c03
commit
055a2c0ef7
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -83,6 +83,7 @@
|
|||||||
"set": "cpp",
|
"set": "cpp",
|
||||||
"unordered_set": "cpp",
|
"unordered_set": "cpp",
|
||||||
"source_location": "cpp",
|
"source_location": "cpp",
|
||||||
"shared_mutex": "cpp"
|
"shared_mutex": "cpp",
|
||||||
|
"strstream": "cpp"
|
||||||
},
|
},
|
||||||
}
|
}
|
@ -1,142 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
class Expr;
|
|
||||||
class Type;
|
|
||||||
|
|
||||||
typedef std::pair<std::string, Type *> FnArg;
|
|
||||||
|
|
||||||
class ASTNode
|
|
||||||
{
|
|
||||||
};
|
|
||||||
|
|
||||||
class Stmt : public ASTNode
|
|
||||||
{
|
|
||||||
};
|
|
||||||
|
|
||||||
class Module : public ASTNode
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Module(const std::vector<Stmt *> &stmts) : stmts(std::move(stmts)) {}
|
|
||||||
const std::vector<Stmt *> stmts;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ValDecl : public Stmt
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
ValDecl(const std::string &name, const Type *type, const Expr *value)
|
|
||||||
: name(name), type(type), value(value) {}
|
|
||||||
|
|
||||||
const std::string name;
|
|
||||||
const Type *type;
|
|
||||||
const Expr *value;
|
|
||||||
};
|
|
||||||
|
|
||||||
class FnDecl : public Stmt
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
FnDecl(const std::string &name, const std::vector<FnArg> &args, const Type *returnType, const Expr *body)
|
|
||||||
: name(name), args(std::move(args)), returnType(returnType), body(body) {}
|
|
||||||
|
|
||||||
const std::string name;
|
|
||||||
const std::vector<FnArg> args;
|
|
||||||
const Type *returnType;
|
|
||||||
const Expr *body;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Type : public ASTNode
|
|
||||||
{
|
|
||||||
};
|
|
||||||
|
|
||||||
class TupleType : public Type
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
TupleType(const std::vector<Type *> &types) : types(types) {}
|
|
||||||
const std::vector<Type *> types;
|
|
||||||
};
|
|
||||||
|
|
||||||
class FunctionType : public Type
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
FunctionType(const std::vector<Type *> &from, const Type *to)
|
|
||||||
: from(std::move(from)), to(to) {}
|
|
||||||
|
|
||||||
const std::vector<Type *> from;
|
|
||||||
const Type *to;
|
|
||||||
};
|
|
||||||
|
|
||||||
class NamedType : public Type
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
NamedType(const std::string &name) : name(name) {}
|
|
||||||
const std::string name;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Expr : public ASTNode
|
|
||||||
{
|
|
||||||
};
|
|
||||||
|
|
||||||
class CallExpr : public Expr
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
CallExpr(const Expr *callee, const std::vector<Expr *> &args)
|
|
||||||
: callee(callee), args(std::move(args)) {}
|
|
||||||
|
|
||||||
const Expr *callee;
|
|
||||||
const std::vector<Expr *> args;
|
|
||||||
};
|
|
||||||
|
|
||||||
class UnaryExpr : public Expr
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
UnaryExpr(const std::string &op, const Expr *expr)
|
|
||||||
: op(op), expr(expr) {}
|
|
||||||
|
|
||||||
const std::string op;
|
|
||||||
const Expr *expr;
|
|
||||||
};
|
|
||||||
|
|
||||||
class BinExpr : public Expr
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
BinExpr(const Expr *left, const std::string &op, const Expr *right)
|
|
||||||
: left(left), op(op), right(right) {}
|
|
||||||
|
|
||||||
const Expr *left;
|
|
||||||
const std::string op;
|
|
||||||
const Expr *right;
|
|
||||||
};
|
|
||||||
|
|
||||||
class PrefExpr : public Expr
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
PrefExpr(const Expr *expr) : expr(expr) {}
|
|
||||||
|
|
||||||
const Expr *expr;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Identifier : public Expr
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Identifier(const std::string &name) : name(name) {}
|
|
||||||
|
|
||||||
const std::string name;
|
|
||||||
};
|
|
||||||
|
|
||||||
class IntValue : public Expr
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
IntValue(const long value) : value(value) {}
|
|
||||||
|
|
||||||
const long value;
|
|
||||||
};
|
|
||||||
|
|
||||||
class FloatValue : public Expr
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
FloatValue(const double value) : value(value) {}
|
|
||||||
|
|
||||||
const double value;
|
|
||||||
};
|
|
22
compiler/include/AST/Base.h
Normal file
22
compiler/include/AST/Base.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
class ASTNode
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~ASTNode() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Expr : public ASTNode
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
class Stmt : public ASTNode
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
class Type : public ASTNode
|
||||||
|
{
|
||||||
|
};
|
||||||
|
}
|
21
compiler/include/AST/Def.h
Normal file
21
compiler/include/AST/Def.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Base.h"
|
||||||
|
|
||||||
|
#include "Expr/BinExpr.h"
|
||||||
|
#include "Expr/Block.h"
|
||||||
|
#include "Expr/Call.h"
|
||||||
|
#include "Expr/Identifier.h"
|
||||||
|
#include "Expr/UnaryExpr.h"
|
||||||
|
#include "Expr/Value.h"
|
||||||
|
|
||||||
|
#include "Stmt/ExprStmt.h"
|
||||||
|
#include "Stmt/FnDecl.h"
|
||||||
|
#include "Stmt/ValDecl.h"
|
||||||
|
|
||||||
|
#include "Type/Function.h"
|
||||||
|
#include "Type/Named.h"
|
||||||
|
#include "Type/Tuple.h"
|
||||||
|
|
||||||
|
#include "Module/Import.h"
|
||||||
|
#include "Module/Module.h"
|
34
compiler/include/AST/Expr/BinExpr.h
Normal file
34
compiler/include/AST/Expr/BinExpr.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AST/Base.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
class BinExpr : public Expr
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BinExpr(const Expr *left, const std::string &op, const Expr *right)
|
||||||
|
: left(left), op(op), right(right) {}
|
||||||
|
|
||||||
|
~BinExpr()
|
||||||
|
{
|
||||||
|
delete left;
|
||||||
|
delete right;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Expr *left;
|
||||||
|
const std::string op;
|
||||||
|
const Expr *right;
|
||||||
|
};
|
||||||
|
|
||||||
|
class PrefExpr : public Expr
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
PrefExpr(const Expr *expr) : expr(expr) {}
|
||||||
|
|
||||||
|
~PrefExpr() { delete expr; }
|
||||||
|
|
||||||
|
const Expr *expr;
|
||||||
|
};
|
||||||
|
}
|
20
compiler/include/AST/Expr/Block.h
Normal file
20
compiler/include/AST/Expr/Block.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AST/Base.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
class BlockExpr : public Expr
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BlockExpr(const std::vector<Stmt *> &stmts) : stmts(std::move(stmts)) {}
|
||||||
|
~BlockExpr()
|
||||||
|
{
|
||||||
|
for (auto &stmt : stmts)
|
||||||
|
delete stmt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<Stmt *> stmts;
|
||||||
|
};
|
||||||
|
}
|
23
compiler/include/AST/Expr/Call.h
Normal file
23
compiler/include/AST/Expr/Call.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AST/Base.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
class CallExpr : public Expr
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CallExpr(const Expr *callee, const std::vector<Expr *> &args)
|
||||||
|
: callee(callee), args(std::move(args)) {}
|
||||||
|
|
||||||
|
~CallExpr()
|
||||||
|
{
|
||||||
|
for (auto &arg : args)
|
||||||
|
delete arg;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Expr *callee;
|
||||||
|
const std::vector<Expr *> args;
|
||||||
|
};
|
||||||
|
}
|
15
compiler/include/AST/Expr/Identifier.h
Normal file
15
compiler/include/AST/Expr/Identifier.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AST/Base.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
class Identifier : public Expr
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Identifier(const std::string &name) : name(name) {}
|
||||||
|
|
||||||
|
const std::string name;
|
||||||
|
};
|
||||||
|
}
|
19
compiler/include/AST/Expr/UnaryExpr.h
Normal file
19
compiler/include/AST/Expr/UnaryExpr.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AST/Base.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
class UnaryExpr : public Expr
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
UnaryExpr(const std::string &op, const Expr *expr)
|
||||||
|
: op(op), expr(expr) {}
|
||||||
|
|
||||||
|
~UnaryExpr() { delete expr; }
|
||||||
|
|
||||||
|
const std::string op;
|
||||||
|
const Expr *expr;
|
||||||
|
};
|
||||||
|
}
|
21
compiler/include/AST/Expr/Value.h
Normal file
21
compiler/include/AST/Expr/Value.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AST/Base.h"
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
class IntValue : public Expr
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
IntValue(int64_t value) : value(value) {}
|
||||||
|
const int64_t value;
|
||||||
|
};
|
||||||
|
|
||||||
|
class FloatValue : public Expr
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
FloatValue(double value) : value(value) {}
|
||||||
|
const double value;
|
||||||
|
};
|
||||||
|
}
|
14
compiler/include/AST/Module/Import.h
Normal file
14
compiler/include/AST/Module/Import.h
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AST/Base.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
class Import : public ASTNode
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Import(const std::string &moduleName) : moduleName(moduleName) {}
|
||||||
|
const std::string moduleName;
|
||||||
|
};
|
||||||
|
}
|
28
compiler/include/AST/Module/Module.h
Normal file
28
compiler/include/AST/Module/Module.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AST/Base.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
class Import;
|
||||||
|
|
||||||
|
class Module : public ASTNode
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Module(const std::vector<Import *> &imports, const std::vector<Stmt *> &stmts)
|
||||||
|
: imports(std::move(imports)), stmts(std::move(stmts)) {}
|
||||||
|
|
||||||
|
~Module()
|
||||||
|
{
|
||||||
|
for (auto &import : imports)
|
||||||
|
delete import;
|
||||||
|
|
||||||
|
for (auto &stmt : stmts)
|
||||||
|
delete stmt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<Import *> &imports;
|
||||||
|
const std::vector<Stmt *> stmts;
|
||||||
|
};
|
||||||
|
}
|
15
compiler/include/AST/Stmt/ExprStmt.h
Normal file
15
compiler/include/AST/Stmt/ExprStmt.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AST/Base.h"
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
class ExprStmt : public Stmt
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ExprStmt(const Expr *expr) : expr(expr) {}
|
||||||
|
~ExprStmt() { delete expr; }
|
||||||
|
|
||||||
|
const Expr *expr;
|
||||||
|
};
|
||||||
|
}
|
31
compiler/include/AST/Stmt/FnDecl.h
Normal file
31
compiler/include/AST/Stmt/FnDecl.h
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AST/Base.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
typedef std::pair<std::string, Type *> FnArg;
|
||||||
|
|
||||||
|
class FnDecl : public Stmt
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
FnDecl(const std::string &name, const std::vector<FnArg> &args, const Type *returnType, const Expr *body)
|
||||||
|
: name(name), args(std::move(args)), returnType(returnType), body(body) {}
|
||||||
|
|
||||||
|
~FnDecl()
|
||||||
|
{
|
||||||
|
for (auto &arg : args)
|
||||||
|
delete arg.second;
|
||||||
|
|
||||||
|
delete returnType;
|
||||||
|
delete body;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string name;
|
||||||
|
const std::vector<FnArg> args;
|
||||||
|
const Type *returnType;
|
||||||
|
const Expr *body;
|
||||||
|
};
|
||||||
|
}
|
24
compiler/include/AST/Stmt/ValDecl.h
Normal file
24
compiler/include/AST/Stmt/ValDecl.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AST/Base.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
class ValDecl : public Stmt
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ValDecl(const std::string &name, const Type *type, const Expr *value)
|
||||||
|
: name(name), type(type), value(value) {}
|
||||||
|
|
||||||
|
~ValDecl()
|
||||||
|
{
|
||||||
|
delete type;
|
||||||
|
delete value;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string name;
|
||||||
|
const Type *type;
|
||||||
|
const Expr *value;
|
||||||
|
};
|
||||||
|
}
|
25
compiler/include/AST/Type/Function.h
Normal file
25
compiler/include/AST/Type/Function.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AST/Base.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
class FunctionType : public Type
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
FunctionType(const std::vector<Type *> &from, const Type *to)
|
||||||
|
: from(std::move(from)), to(to) {}
|
||||||
|
|
||||||
|
~FunctionType()
|
||||||
|
{
|
||||||
|
for (auto &arg : from)
|
||||||
|
delete arg;
|
||||||
|
|
||||||
|
delete to;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<Type *> from;
|
||||||
|
const Type *to;
|
||||||
|
};
|
||||||
|
}
|
14
compiler/include/AST/Type/Named.h
Normal file
14
compiler/include/AST/Type/Named.h
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AST/Base.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
class NamedType : public Type
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
NamedType(const std::string &name) : name(name) {}
|
||||||
|
const std::string name;
|
||||||
|
};
|
||||||
|
}
|
21
compiler/include/AST/Type/Tuple.h
Normal file
21
compiler/include/AST/Type/Tuple.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "AST/Def.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace ast
|
||||||
|
{
|
||||||
|
class TupleType : public Type
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TupleType(const std::vector<Type *> &types) : types(std::move(types)) {}
|
||||||
|
|
||||||
|
~TupleType()
|
||||||
|
{
|
||||||
|
for (auto &type : types)
|
||||||
|
delete type;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<Type *> types;
|
||||||
|
};
|
||||||
|
}
|
@ -1,22 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
#include "AST.h"
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
class ASTVisitor
|
|
||||||
{
|
|
||||||
virtual T visit(const ASTNode *node)
|
|
||||||
{
|
|
||||||
if (IntValue *cnode = dynamic_cast<IntValue *>(node))
|
|
||||||
return visitIntValue(cnode);
|
|
||||||
else if (FloatValue *cnode = dynamic_cast<FloatValue *>(node))
|
|
||||||
return visitFloatValue(cnode);
|
|
||||||
|
|
||||||
throw std::logic_error("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual T visitIntValue(const IntValue *node);
|
|
||||||
virtual T visitFloatValue(const IntValue *node);
|
|
||||||
};
|
|
@ -2,13 +2,14 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "AST.h"
|
#include "AST/Def.h"
|
||||||
#include "parser.gen.h"
|
#include "parser.gen.h"
|
||||||
|
|
||||||
class ParserDriver
|
class ParserDriver
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ParserDriver() {}
|
ParserDriver() {}
|
||||||
|
~ParserDriver() { delete module; }
|
||||||
|
|
||||||
int parse(const std::string &input);
|
int parse(const std::string &input);
|
||||||
|
|
||||||
@ -17,7 +18,7 @@ public:
|
|||||||
|
|
||||||
std::string file;
|
std::string file;
|
||||||
yy::location location;
|
yy::location location;
|
||||||
Module *module;
|
ast::Module *module;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define YY_DECL yy::parser::symbol_type yylex(ParserDriver &driver)
|
#define YY_DECL yy::parser::symbol_type yylex(ParserDriver &driver)
|
||||||
|
@ -35,19 +35,24 @@ whitespace [ \n\t\r\v]+
|
|||||||
if (text == "->") return _token(RARR);
|
if (text == "->") return _token(RARR);
|
||||||
return _token(OPERATOR); }
|
return _token(OPERATOR); }
|
||||||
|
|
||||||
|
"." { return _token(DOT); }
|
||||||
"," { return _token(COMMA); }
|
"," { return _token(COMMA); }
|
||||||
";" { return _token(SEMI); }
|
";" { return _token(SEMI); }
|
||||||
"(" { return _token(LPAREN); }
|
"(" { return _token(LPAREN); }
|
||||||
")" { return _token(RPAREN); }
|
")" { return _token(RPAREN); }
|
||||||
"[" { return _token(LBRACKET); }
|
"[" { return _token(LBRACKET); }
|
||||||
"]" { return _token(RBRACKET); }
|
"]" { return _token(RBRACKET); }
|
||||||
|
"{" { return _token(LBRACE); }
|
||||||
|
"}" { return _token(RBRACE); }
|
||||||
|
|
||||||
"fn" { return _token(FN); }
|
|
||||||
"unop" { return _token(UNOP); }
|
|
||||||
"binop" { return _token(BINOP); }
|
"binop" { return _token(BINOP); }
|
||||||
"val" { return _token(VAL); }
|
|
||||||
"import" { return _token(IMPORT); }
|
|
||||||
"declare" { return _token(DECLARE); }
|
"declare" { return _token(DECLARE); }
|
||||||
|
"fn" { return _token(FN); }
|
||||||
|
"import" { return _token(IMPORT); }
|
||||||
|
"trait" { return _token(TRAIT); }
|
||||||
|
"type" { return _token(TYPE); }
|
||||||
|
"unop" { return _token(UNOP); }
|
||||||
|
"val" { return _token(VAL); }
|
||||||
|
|
||||||
|
|
||||||
{letter}({digit}|{letter})* { return _token(IDENTIFIER); }
|
{letter}({digit}|{letter})* { return _token(IDENTIFIER); }
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
%define api.location.include {"location.gen.h"}
|
%define api.location.include {"location.gen.h"}
|
||||||
|
|
||||||
%code requires {
|
%code requires {
|
||||||
#include "AST.h"
|
#include "AST/Def.h"
|
||||||
class ParserDriver;
|
class ParserDriver;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,43 +38,57 @@ class ParserDriver;
|
|||||||
|
|
||||||
RARR "->"
|
RARR "->"
|
||||||
EQUALS "="
|
EQUALS "="
|
||||||
|
DOT "."
|
||||||
COMMA ","
|
COMMA ","
|
||||||
SEMI ";"
|
SEMI ";"
|
||||||
LPAREN "("
|
LPAREN "("
|
||||||
RPAREN ")"
|
RPAREN ")"
|
||||||
LBRACKET "["
|
LBRACKET "["
|
||||||
RBRACKET "]"
|
RBRACKET "]"
|
||||||
|
LBRACE "{"
|
||||||
|
RBRACE "}"
|
||||||
|
|
||||||
FN "fn"
|
|
||||||
UNOP "unop"
|
|
||||||
BINOP "binop"
|
BINOP "binop"
|
||||||
VAL "val"
|
|
||||||
IMPORT "import"
|
|
||||||
DECLARE "declare"
|
DECLARE "declare"
|
||||||
|
FN "fn"
|
||||||
|
IMPORT "import"
|
||||||
|
TRAIT "trait"
|
||||||
|
TYPE "type"
|
||||||
|
UNOP "unop"
|
||||||
|
VAL "val"
|
||||||
;
|
;
|
||||||
|
|
||||||
%token END 0 "end of file"
|
%token END 0 "end of file"
|
||||||
|
|
||||||
%type <std::vector<Stmt *>> stmts;
|
%type <std::vector<ast::Stmt *>>
|
||||||
%type <Stmt *>
|
topLevelStmts
|
||||||
stmt
|
blockStmts
|
||||||
decl
|
|
||||||
;
|
;
|
||||||
|
|
||||||
%type <FnDecl *> fnDecl;
|
%type <ast::Stmt *>
|
||||||
%type <ValDecl *> valDecl;
|
topLevelStmt
|
||||||
|
blockStmt
|
||||||
|
;
|
||||||
|
|
||||||
%type <Type *>
|
%type <ast::FnDecl *> extFnDecl fnDecl;
|
||||||
|
%type <ast::ValDecl *> valDecl;
|
||||||
|
|
||||||
|
%type <std::vector<ast::Type *>>
|
||||||
|
types
|
||||||
|
tupleTypes
|
||||||
|
fnTypeArgs
|
||||||
|
;
|
||||||
|
|
||||||
|
%type <ast::Type *>
|
||||||
type
|
type
|
||||||
type0
|
|
||||||
;
|
;
|
||||||
|
|
||||||
%type <std::vector<Expr *>>
|
%type <std::vector<ast::Expr *>>
|
||||||
exprs
|
exprs
|
||||||
args
|
args
|
||||||
;
|
;
|
||||||
|
|
||||||
%type <Expr *>
|
%type <ast::Expr *>
|
||||||
expr
|
expr
|
||||||
expr3
|
expr3
|
||||||
expr2
|
expr2
|
||||||
@ -83,8 +97,8 @@ class ParserDriver;
|
|||||||
literal
|
literal
|
||||||
;
|
;
|
||||||
|
|
||||||
%type <FnArg> fnArg;
|
%type <ast::FnArg> fnArg;
|
||||||
%type <std::vector<FnArg>>
|
%type <std::vector<ast::FnArg>>
|
||||||
fnArgs
|
fnArgs
|
||||||
fnArgs0
|
fnArgs0
|
||||||
;
|
;
|
||||||
@ -95,53 +109,100 @@ class ParserDriver;
|
|||||||
;
|
;
|
||||||
|
|
||||||
%type <std::string>
|
%type <std::string>
|
||||||
|
moduleName
|
||||||
operator
|
operator
|
||||||
identifier
|
identifier
|
||||||
keyword
|
keyword
|
||||||
;
|
;
|
||||||
|
|
||||||
|
%type <ast::Import *> import;
|
||||||
|
%type <std::vector<ast::Import *>>
|
||||||
|
imports0
|
||||||
|
imports
|
||||||
|
;
|
||||||
|
|
||||||
%start module
|
%start module
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
module: stmts END { driver.module = new Module($1); };
|
module: imports topLevelStmts { driver.module = new ast::Module($2); }
|
||||||
|
|
||||||
stmts: stmt { $$ = std::vector<Stmt *>(); $$.push_back($1); }
|
|
||||||
| stmts stmt { $1.push_back($2); $$ = std::move($1); }
|
|
||||||
;
|
;
|
||||||
|
|
||||||
stmt: decl;
|
|
||||||
|
|
||||||
decl: valDecl { $$ = $1; }
|
imports: %empty { ; }
|
||||||
|
| imports0 { $$ = std::move($1); }
|
||||||
|
;
|
||||||
|
|
||||||
|
imports0: import { $$.push_back($1); }
|
||||||
|
| imports0 import { $1.push_back($2); $$ = std::move($1); }
|
||||||
|
;
|
||||||
|
|
||||||
|
import: IMPORT moduleName ";" { $$ = new ast::Import($2); }
|
||||||
|
;
|
||||||
|
|
||||||
|
moduleName: identifier { $$ = $1; }
|
||||||
|
| moduleName "." identifier { $$ = $1 + $2 + $3; }
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
topLevelStmts: topLevelStmt { $$.push_back($1); }
|
||||||
|
| topLevelStmts topLevelStmt { $1.push_back($2); $$ = std::move($1); }
|
||||||
|
;
|
||||||
|
|
||||||
|
topLevelStmt: fnDecl { $$ = $1; }
|
||||||
|
| extFnDecl { $$ = $1; }
|
||||||
|
| valDecl { $$ = $1; }
|
||||||
|
;
|
||||||
|
|
||||||
|
blockStmts: blockStmt { $$.push_back($1); }
|
||||||
|
| blockStmts blockStmt { $1.push_back($2); $$ = std::move($1); }
|
||||||
|
;
|
||||||
|
|
||||||
|
blockStmt: valDecl { $$ = $1; }
|
||||||
| fnDecl { $$ = $1; }
|
| fnDecl { $$ = $1; }
|
||||||
|
| expr ";" { $$ = new ast::ExprStmt($1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
valDecl: VAL identifier "=" expr ";" { $$ = new ValDecl($2, nullptr, $4); }
|
valDecl: VAL identifier "=" expr ";" { $$ = new ast::ValDecl($2, nullptr, $4); }
|
||||||
| VAL identifier type "=" expr ";" { $$ = new ValDecl($2, $3, $5); }
|
| VAL identifier type "=" expr ";" { $$ = new ast::ValDecl($2, $3, $5); }
|
||||||
;
|
;
|
||||||
|
|
||||||
fnDecl: FN identifier "[" fnArgs "]" type "=" expr ";" { $$ = new FnDecl($2, $4, $6, $8); }
|
fnDecl: FN identifier "[" fnArgs "]" type "=" expr ";" { $$ = new ast::FnDecl($2, $4, $6, $8); }
|
||||||
;
|
;
|
||||||
|
|
||||||
fnArgs: %empty { $$ = std::vector<FnArg>(); }
|
extFnDecl: DECLARE FN identifier "[" fnArgs "]" type ";" { $$ = new ast::FnDecl($3, $5, $7, nullptr); }
|
||||||
|
;
|
||||||
|
|
||||||
|
fnArgs: %empty { ; }
|
||||||
| fnArgs0 { $$ = std::move($1); }
|
| fnArgs0 { $$ = std::move($1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
fnArgs0: fnArg { $$ = std::vector<FnArg>(); $$.push_back($1); }
|
fnArgs0: fnArg { $$.push_back($1); }
|
||||||
| fnArgs0 "," fnArg { $$.push_back($3); $$ = std::move($1); }
|
| fnArgs0 "," fnArg { $$.push_back($3); $$ = std::move($1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
fnArg: identifier type { $$ = FnArg($1, $2); }
|
fnArg: identifier type { $$ = ast::FnArg($1, $2); }
|
||||||
;
|
;
|
||||||
|
|
||||||
type: type0
|
types: type { $$.push_back($1); }
|
||||||
|
| types "," type { $1.push_back($3); $$ = std::move($1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
type0: identifier { $$ = new NamedType($1); }
|
type: identifier { $$ = new ast::NamedType($1); }
|
||||||
| "(" type ")" { $$ = $2; }
|
| "(" type ")" { $$ = $2; }
|
||||||
|
| "(" tupleTypes ")" { $$ = new ast::TupleType($2); }
|
||||||
|
| "[" fnTypeArgs "]" "->" type { $$ = new ast::FunctionType($2, $5); }
|
||||||
;
|
;
|
||||||
|
|
||||||
exprs: expr { $$ = std::vector<Expr *>(); $$.push_back($1); }
|
tupleTypes: type "," type { $$.push_back($1); $$.push_back($3); }
|
||||||
|
| tupleTypes "," type { $1.push_back($3); $$ = std::move($1); }
|
||||||
|
;
|
||||||
|
|
||||||
|
fnTypeArgs: %empty { ; }
|
||||||
|
| types { $$ = std::move($1); }
|
||||||
|
;
|
||||||
|
|
||||||
|
exprs: expr { $$.push_back($1); }
|
||||||
| exprs "," expr { $1.push_back($3); $$ = std::move($1); }
|
| exprs "," expr { $1.push_back($3); $$ = std::move($1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -150,54 +211,55 @@ expr: expr3 { $$ = $1; }
|
|||||||
|
|
||||||
expr3: expr2 { $$ = $1; }
|
expr3: expr2 { $$ = $1; }
|
||||||
| "[" lambdaArgs "]" "->" expr { $$ = NULL; }
|
| "[" lambdaArgs "]" "->" expr { $$ = NULL; }
|
||||||
|
| "{" blockStmts "}" { $$ = new ast::BlockExpr($2); }
|
||||||
;
|
;
|
||||||
|
|
||||||
expr2: expr1 { $$ = $1; }
|
expr2: expr1 { $$ = $1; }
|
||||||
| expr2 operator expr1 { $$ = new BinExpr($1, $2, $3); }
|
| expr2 operator expr1 { $$ = new ast::BinExpr($1, $2, $3); }
|
||||||
;
|
;
|
||||||
|
|
||||||
expr1: expr0 { $$ = $1; }
|
expr1: expr0 { $$ = $1; }
|
||||||
| operator expr1 { $$ = new UnaryExpr($1, $2); }
|
| operator expr1 { $$ = new ast::UnaryExpr($1, $2); }
|
||||||
;
|
;
|
||||||
|
|
||||||
expr0: literal { $$ = $1; }
|
expr0: literal { $$ = $1; }
|
||||||
| identifier { $$ = new Identifier($1); }
|
| identifier { $$ = new ast::Identifier($1); }
|
||||||
| expr0 "[" args "]" { $$ = new CallExpr($1, $3); }
|
| expr0 "[" args "]" { $$ = new ast::CallExpr($1, $3); }
|
||||||
| "(" expr ")" { $$ = new PrefExpr($2); }
|
| "(" expr ")" { $$ = new ast::PrefExpr($2); }
|
||||||
;
|
;
|
||||||
|
|
||||||
lambdaArgs: %empty { $$ = std::vector<std::string>(); }
|
lambdaArgs: %empty { ; }
|
||||||
| identifiers { $$ = std::move($1); }
|
| identifiers { $$ = std::move($1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
args: %empty { $$ = std::vector<Expr *>(); }
|
args: %empty { ; }
|
||||||
| exprs { $$ = std::move($1); }
|
| exprs { $$ = std::move($1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
literal: INT { $$ = new IntValue($1); }
|
literal: INT { $$ = new ast::IntValue($1); }
|
||||||
| FLOAT { $$ = new FloatValue($1); }
|
| FLOAT { $$ = new ast::FloatValue($1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
operator: "->" { $$ = $1; }
|
operator: "->"
|
||||||
| "=" { $$ = $1; }
|
| "="
|
||||||
| OPERATOR { $$ = $1; }
|
| OPERATOR
|
||||||
;
|
;
|
||||||
|
|
||||||
identifiers: identifier { $$ = std::vector<std::string>(); $$.push_back($1); }
|
identifiers: identifier { $$.push_back($1); }
|
||||||
| identifiers "," identifier { $1.push_back($3); $$ = std::move($1); }
|
| identifiers "," identifier { $1.push_back($3); $$ = std::move($1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
identifier: keyword { $$ = $1; }
|
identifier: keyword
|
||||||
| IDENTIFIER { $$ = $1; }
|
| IDENTIFIER
|
||||||
;
|
;
|
||||||
|
|
||||||
keyword: FN { $$ = $1; }
|
keyword: FN
|
||||||
| VAL { $$ = $1; }
|
| VAL
|
||||||
| UNOP { $$ = $1; }
|
| UNOP
|
||||||
| BINOP { $$ = $1; }
|
| BINOP
|
||||||
| IMPORT { $$ = $1; }
|
| IMPORT
|
||||||
| DECLARE { $$ = $1; }
|
| DECLARE
|
||||||
;
|
;
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
@ -25,6 +25,6 @@ int main(int argc, char *argv[])
|
|||||||
auto parser = ParserDriver();
|
auto parser = ParserDriver();
|
||||||
parser.parse(buf.str());
|
parser.parse(buf.str());
|
||||||
|
|
||||||
auto fn = (FnDecl *)parser.module->stmts.at(0);
|
auto fn = (ast::FnDecl *)parser.module->stmts.at(0);
|
||||||
std::cout << fn->name << std::endl;
|
std::cout << fn->name << std::endl;
|
||||||
}
|
}
|
||||||
|
@ -1 +1,9 @@
|
|||||||
|
import test.test;
|
||||||
|
|
||||||
fn main[] Int = 42;
|
fn main[] Int = 42;
|
||||||
|
|
||||||
|
fn test[b Int] Int = {
|
||||||
|
fn helper[] Int = 10;
|
||||||
|
20 * b;
|
||||||
|
};
|
||||||
|
|
||||||
|
@ -1,29 +1,33 @@
|
|||||||
type Bool = class {
|
type Bool = {
|
||||||
declare unop ! Bool;
|
declare unop ! Bool;
|
||||||
|
|
||||||
declare binop &&(b Bool) Bool;
|
declare binop &&[b Bool] Bool;
|
||||||
declare binop ||(b Bool) Bool;
|
declare binop ||[b Bool] Bool;
|
||||||
};
|
};
|
||||||
|
|
||||||
type Int = class {
|
type Int = {
|
||||||
declare unop + Int;
|
declare unop + Int;
|
||||||
declare unop - Int;
|
declare unop - Int;
|
||||||
|
|
||||||
declare binop ==(Int) Bool;
|
declare binop ==[Int] Bool;
|
||||||
declare binop !=(Int) Bool;
|
declare binop !=[Int] Bool;
|
||||||
declare binop >(Int) Bool;
|
declare binop >[Int] Bool;
|
||||||
declare binop <(Int) Bool;
|
declare binop <[Int] Bool;
|
||||||
declare binop >=(Int) Bool;
|
declare binop >=[Int] Bool;
|
||||||
declare binop <=(Int) Bool;
|
declare binop <=[Int] Bool;
|
||||||
|
|
||||||
declare binop +(Int) Int;
|
declare binop +[Int] Int;
|
||||||
declare binop -(Int) Int;
|
declare binop -[Int] Int;
|
||||||
declare binop *(Int) Int;
|
declare binop *[Int] Int;
|
||||||
declare binop /(Int) Int;
|
declare binop /[Int] Int;
|
||||||
declare binop %(Int) Int;
|
declare binop %[Int] Int;
|
||||||
|
|
||||||
declare factory (f Float);
|
declare factory [f Float];
|
||||||
declare factory ();
|
declare factory [];
|
||||||
|
|
||||||
declare fn str() String;
|
declare fn str[] String;
|
||||||
|
};
|
||||||
|
|
||||||
|
trait List{T} = {
|
||||||
|
fn
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user