diff --git a/libs/ast/include/pslang/ast/expression.hpp b/libs/ast/include/pslang/ast/expression.hpp index 8500c3f..c5ff990 100644 --- a/libs/ast/include/pslang/ast/expression.hpp +++ b/libs/ast/include/pslang/ast/expression.hpp @@ -28,6 +28,12 @@ namespace pslang::ast ast::location location; }; + struct constructor + { + type_ptr type; + std::vector args; + }; + using expression_impl = std::variant< literal, identifier, diff --git a/libs/ast/source/resolve_identifiers.cpp b/libs/ast/source/resolve_identifiers.cpp index 9e180e6..1a30abc 100644 --- a/libs/ast/source/resolve_identifiers.cpp +++ b/libs/ast/source/resolve_identifiers.cpp @@ -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) { diff --git a/libs/interpreter/source/eval.cpp b/libs/interpreter/source/eval.cpp index d23e751..386e889 100644 --- a/libs/interpreter/source/eval.cpp +++ b/libs/interpreter/source/eval.cpp @@ -510,6 +510,9 @@ namespace pslang::interpreter auto identifier = std::get_if(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()) diff --git a/libs/parser/rules/pslang.y b/libs/parser/rules/pslang.y index 7bba141..f6e30a1 100644 --- a/libs/parser/rules/pslang.y +++ b/libs/parser/rules/pslang.y @@ -284,6 +284,18 @@ postfix_expression | postfix_expression lbracket expression rbracket { $$ = ast::array_access{std::make_unique($1), std::make_unique($3), @$}; } | postfix_expression dot name { $$ = ast::field_access{std::make_unique($1), $3, @$}; } | postfix_expression lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique($1), $3, @$}; } +| unit lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique(ast::identifier{"unit"}), $3, @$}; } +| bool lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique(ast::identifier{"bool"}), $3, @$}; } +| i8 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique(ast::identifier{"i8"}), $3, @$}; } +| u8 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique(ast::identifier{"u8"}), $3, @$}; } +| i16 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique(ast::identifier{"i16"}), $3, @$}; } +| u16 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique(ast::identifier{"u16"}), $3, @$}; } +| i32 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique(ast::identifier{"i32"}), $3, @$}; } +| u32 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique(ast::identifier{"u32"}), $3, @$}; } +| i64 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique(ast::identifier{"i64"}), $3, @$}; } +| u64 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique(ast::identifier{"u64"}), $3, @$}; } +| f32 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique(ast::identifier{"f32"}), $3, @$}; } +| f64 lparen comma_separated_expression_list rparen { $$ = ast::function_call{std::make_unique(ast::identifier{"f64"}), $3, @$}; } ; base_expression diff --git a/libs/types/include/pslang/types/type_fwd.hpp b/libs/types/include/pslang/types/type_fwd.hpp index 577febb..b4dacdc 100644 --- a/libs/types/include/pslang/types/type_fwd.hpp +++ b/libs/types/include/pslang/types/type_fwd.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include 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); + } diff --git a/libs/types/source/type.cpp b/libs/types/source/type.cpp index 5921287..dbdb3f6 100644 --- a/libs/types/source/type.cpp +++ b/libs/types/source/type.cpp @@ -8,4 +8,34 @@ namespace pslang::types return static_cast(t1) == static_cast(t2); } + type_ptr builtin_type(std::string const & name) + { + if (name == "unit") + return std::make_unique(unit_type{}); + else if (name == "bool") + return std::make_unique(primitive_type{bool_type{}}); + else if (name == "i8") + return std::make_unique(primitive_type{i8_type{}}); + else if (name == "u8") + return std::make_unique(primitive_type{u8_type{}}); + else if (name == "i16") + return std::make_unique(primitive_type{i16_type{}}); + else if (name == "u16") + return std::make_unique(primitive_type{u16_type{}}); + else if (name == "i32") + return std::make_unique(primitive_type{i32_type{}}); + else if (name == "u32") + return std::make_unique(primitive_type{u32_type{}}); + else if (name == "i64") + return std::make_unique(primitive_type{i64_type{}}); + else if (name == "u64") + return std::make_unique(primitive_type{u64_type{}}); + else if (name == "f32") + return std::make_unique(primitive_type{f32_type{}}); + else if (name == "f64") + return std::make_unique(primitive_type{f64_type{}}); + else + return nullptr; + } + }