Add ref and deref
This commit is contained in:
parent
48a3eb0e85
commit
78c5ab4c25
12
src/ast.cpp
12
src/ast.cpp
@ -49,6 +49,18 @@ namespace AST {
|
|||||||
return out.str();
|
return out.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string RefExpression::formatted() {
|
||||||
|
std::stringstream out{ "" };
|
||||||
|
out << "&" << this->m_expr->formatted();
|
||||||
|
return out.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string DerefExpression::formatted() {
|
||||||
|
std::stringstream out{ "" };
|
||||||
|
out << "*" << this->m_expr->formatted();
|
||||||
|
return out.str();
|
||||||
|
}
|
||||||
|
|
||||||
std::string ExpressionStatement::formatted() {
|
std::string ExpressionStatement::formatted() {
|
||||||
std::stringstream out{ "" };
|
std::stringstream out{ "" };
|
||||||
out << this->m_expr->formatted();
|
out << this->m_expr->formatted();
|
||||||
|
|||||||
48
src/ast.h
48
src/ast.h
@ -39,6 +39,7 @@ namespace AST {
|
|||||||
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) = 0;
|
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// @brief Any integer literal
|
||||||
class IntLiteralExpression : public Expression {
|
class IntLiteralExpression : public Expression {
|
||||||
private:
|
private:
|
||||||
int m_value;
|
int m_value;
|
||||||
@ -61,6 +62,7 @@ namespace AST {
|
|||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// @brief i.e. "contents"
|
||||||
class StringLiteralExpression : public Expression {
|
class StringLiteralExpression : public Expression {
|
||||||
private:
|
private:
|
||||||
std::string m_value;
|
std::string m_value;
|
||||||
@ -76,6 +78,7 @@ namespace AST {
|
|||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// @brief Anything that references by value
|
||||||
class ValueReferenceExpression : public Expression {
|
class ValueReferenceExpression : public Expression {
|
||||||
private:
|
private:
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
@ -91,6 +94,7 @@ namespace AST {
|
|||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// @brief Any binary operation (i.e. lhs + rhs)
|
||||||
class BinaryOperationExpression : public Expression {
|
class BinaryOperationExpression : public Expression {
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Expression> m_lhs;
|
std::unique_ptr<Expression> m_lhs;
|
||||||
@ -117,6 +121,7 @@ namespace AST {
|
|||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// @brief Same as fn()/// @brief
|
||||||
class FunctionCallExpression : public Expression {
|
class FunctionCallExpression : public Expression {
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Expression> m_fn_expr;
|
std::unique_ptr<Expression> m_fn_expr;
|
||||||
@ -140,6 +145,7 @@ namespace AST {
|
|||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// @brief Same as (type) value
|
||||||
class CastExpression : public Expression {
|
class CastExpression : public Expression {
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<types::Type> m_ty;
|
std::shared_ptr<types::Type> m_ty;
|
||||||
@ -163,6 +169,48 @@ namespace AST {
|
|||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// @brief Same as &value
|
||||||
|
class RefExpression : public Expression {
|
||||||
|
private:
|
||||||
|
std::unique_ptr<Expression> m_expr;
|
||||||
|
public:
|
||||||
|
RefExpression(
|
||||||
|
token::Metadata meta,
|
||||||
|
std::unique_ptr<Expression> expr)
|
||||||
|
: Expression{ meta }
|
||||||
|
, m_expr{ std::move(expr) } {
|
||||||
|
}
|
||||||
|
virtual ~RefExpression() override = default;
|
||||||
|
virtual std::string formatted() override;
|
||||||
|
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
|
||||||
|
virtual std::shared_ptr<types::Type> typecheck(
|
||||||
|
typecheck::State& state,
|
||||||
|
typecheck::Scope& scope,
|
||||||
|
std::optional<std::shared_ptr<types::Type>> expected_ty
|
||||||
|
) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @brief Same as *value
|
||||||
|
class DerefExpression : public Expression {
|
||||||
|
private:
|
||||||
|
std::unique_ptr<Expression> m_expr;
|
||||||
|
public:
|
||||||
|
DerefExpression(
|
||||||
|
token::Metadata meta,
|
||||||
|
std::unique_ptr<Expression> expr)
|
||||||
|
: Expression{ meta }
|
||||||
|
, m_expr{ std::move(expr) } {
|
||||||
|
}
|
||||||
|
virtual ~DerefExpression() override = default;
|
||||||
|
virtual std::string formatted() override;
|
||||||
|
virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) override;
|
||||||
|
virtual std::shared_ptr<types::Type> typecheck(
|
||||||
|
typecheck::State& state,
|
||||||
|
typecheck::Scope& scope,
|
||||||
|
std::optional<std::shared_ptr<types::Type>> expected_ty
|
||||||
|
) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class ReturnStatement : public Statement {
|
class ReturnStatement : public Statement {
|
||||||
private:
|
private:
|
||||||
|
|||||||
@ -111,6 +111,27 @@ namespace AST {
|
|||||||
return expr;
|
return expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
codegen::StackValue RefExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||||
|
auto with_lvalue = scope.with_lvalue();
|
||||||
|
return this->m_expr->codegen(builder, with_lvalue);
|
||||||
|
}
|
||||||
|
|
||||||
|
codegen::StackValue DerefExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||||
|
auto value = this->m_expr->codegen(builder, scope);
|
||||||
|
if (value.ty->m_kind == types::TypeKind::Pointer) {
|
||||||
|
auto ptr_ty = dynamic_cast<types::PointerType*>(value.ty.get());
|
||||||
|
auto loaded = ptr_ty->m_inner->load(builder, value.value);
|
||||||
|
return codegen::StackValue{
|
||||||
|
loaded.first,
|
||||||
|
loaded.second
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new CompileError("Tried to deref a non-pointer!", this->m_meta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void ReturnStatement::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
void ReturnStatement::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||||
if (!builder.block)
|
if (!builder.block)
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -253,6 +253,35 @@ namespace AST {
|
|||||||
} };
|
} };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<types::Type> RefExpression::typecheck(
|
||||||
|
typecheck::State& state,
|
||||||
|
typecheck::Scope& scope,
|
||||||
|
std::optional<std::shared_ptr<types::Type>>
|
||||||
|
) {
|
||||||
|
auto expr_ty = this->m_expr->typecheck(state, scope, {});
|
||||||
|
return std::shared_ptr<types::Type> {
|
||||||
|
new types::PointerType{ expr_ty }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<types::Type> DerefExpression::typecheck(
|
||||||
|
typecheck::State& state,
|
||||||
|
typecheck::Scope& scope,
|
||||||
|
std::optional<std::shared_ptr<types::Type>>
|
||||||
|
) {
|
||||||
|
auto expr_ty = this->m_expr->typecheck(state, scope, {});
|
||||||
|
if (expr_ty->m_kind != types::TypeKind::Pointer) {
|
||||||
|
state.errors.push_back(
|
||||||
|
CompileError("Tried to deref " + expr_ty->formatted(), this->m_meta));
|
||||||
|
return std::shared_ptr<types::Type> {
|
||||||
|
new types::FundamentalType{ types::FundamentalTypeKind::Void }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
auto ptr_ty = dynamic_cast<types::PointerType*>(expr_ty.get());
|
||||||
|
return ptr_ty->m_inner;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void ReturnStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
void ReturnStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
||||||
auto res_ty = this->m_expr->typecheck(state, scope, scope.return_ty);
|
auto res_ty = this->m_expr->typecheck(state, scope, scope.return_ty);
|
||||||
if (scope.return_ty) {
|
if (scope.return_ty) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user