Support zero-initialization of built-in types and structs
This commit is contained in:
parent
d33b00e368
commit
5419f87a56
6 changed files with 57 additions and 0 deletions
|
|
@ -28,6 +28,12 @@ namespace pslang::ast
|
|||
ast::location location;
|
||||
};
|
||||
|
||||
struct constructor
|
||||
{
|
||||
type_ptr type;
|
||||
std::vector<expression_ptr> args;
|
||||
};
|
||||
|
||||
using expression_impl = std::variant<
|
||||
literal,
|
||||
identifier,
|
||||
|
|
|
|||
|
|
@ -103,6 +103,9 @@ namespace pslang::ast
|
|||
|
||||
void apply(identifier & identifier)
|
||||
{
|
||||
if (types::builtin_type(identifier.name))
|
||||
return;
|
||||
|
||||
bool crossed_function_scope = false;
|
||||
for (auto it = scopes.rbegin(); it != scopes.rend(); ++it)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -510,6 +510,9 @@ namespace pslang::interpreter
|
|||
auto identifier = std::get_if<ast::identifier>(function_call.function.get());
|
||||
if (identifier)
|
||||
{
|
||||
if (auto type = types::builtin_type(identifier->name))
|
||||
return zero_value(context, *type);
|
||||
|
||||
for (auto it = context.scope_stack.rbegin(); it != context.scope_stack.rend(); ++it)
|
||||
{
|
||||
if (auto jt = it->structs.find(identifier->name); jt != it->structs.end())
|
||||
|
|
|
|||
|
|
@ -284,6 +284,18 @@ postfix_expression
|
|||
| postfix_expression lbracket expression rbracket { $$ = ast::array_access{std::make_unique<ast::expression>($1), std::make_unique<ast::expression>($3), @$}; }
|
||||
| postfix_expression dot name { $$ = ast::field_access{std::make_unique<ast::expression>($1), $3, @$}; }
|
||||
| postfix_expression lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique<ast::expression>($1), $3, @$}; }
|
||||
| unit lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique<ast::expression>(ast::identifier{"unit"}), $3, @$}; }
|
||||
| bool lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique<ast::expression>(ast::identifier{"bool"}), $3, @$}; }
|
||||
| i8 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique<ast::expression>(ast::identifier{"i8"}), $3, @$}; }
|
||||
| u8 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique<ast::expression>(ast::identifier{"u8"}), $3, @$}; }
|
||||
| i16 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique<ast::expression>(ast::identifier{"i16"}), $3, @$}; }
|
||||
| u16 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique<ast::expression>(ast::identifier{"u16"}), $3, @$}; }
|
||||
| i32 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique<ast::expression>(ast::identifier{"i32"}), $3, @$}; }
|
||||
| u32 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique<ast::expression>(ast::identifier{"u32"}), $3, @$}; }
|
||||
| i64 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique<ast::expression>(ast::identifier{"i64"}), $3, @$}; }
|
||||
| u64 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique<ast::expression>(ast::identifier{"u64"}), $3, @$}; }
|
||||
| f32 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique<ast::expression>(ast::identifier{"f32"}), $3, @$}; }
|
||||
| f64 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique<ast::expression>(ast::identifier{"f64"}), $3, @$}; }
|
||||
;
|
||||
|
||||
base_expression
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace pslang::types
|
||||
{
|
||||
|
|
@ -11,4 +12,6 @@ namespace pslang::types
|
|||
|
||||
bool equal(type const & t1, type const & t2);
|
||||
|
||||
type_ptr builtin_type(std::string const & name);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,4 +8,34 @@ namespace pslang::types
|
|||
return static_cast<type_impl const &>(t1) == static_cast<type_impl const &>(t2);
|
||||
}
|
||||
|
||||
type_ptr builtin_type(std::string const & name)
|
||||
{
|
||||
if (name == "unit")
|
||||
return std::make_unique<type>(unit_type{});
|
||||
else if (name == "bool")
|
||||
return std::make_unique<type>(primitive_type{bool_type{}});
|
||||
else if (name == "i8")
|
||||
return std::make_unique<type>(primitive_type{i8_type{}});
|
||||
else if (name == "u8")
|
||||
return std::make_unique<type>(primitive_type{u8_type{}});
|
||||
else if (name == "i16")
|
||||
return std::make_unique<type>(primitive_type{i16_type{}});
|
||||
else if (name == "u16")
|
||||
return std::make_unique<type>(primitive_type{u16_type{}});
|
||||
else if (name == "i32")
|
||||
return std::make_unique<type>(primitive_type{i32_type{}});
|
||||
else if (name == "u32")
|
||||
return std::make_unique<type>(primitive_type{u32_type{}});
|
||||
else if (name == "i64")
|
||||
return std::make_unique<type>(primitive_type{i64_type{}});
|
||||
else if (name == "u64")
|
||||
return std::make_unique<type>(primitive_type{u64_type{}});
|
||||
else if (name == "f32")
|
||||
return std::make_unique<type>(primitive_type{f32_type{}});
|
||||
else if (name == "f64")
|
||||
return std::make_unique<type>(primitive_type{f64_type{}});
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue