summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ast/pattern.cpp147
-rw-r--r--src/ast/pattern.hpp4
-rw-r--r--src/hir/from_ast.cpp7
-rw-r--r--src/main.cpp25
-rw-r--r--src/parse/common.hpp2
-rw-r--r--src/parse/expr.cpp2
-rw-r--r--src/parse/lex.cpp35
-rw-r--r--src/parse/paths.cpp10
-rw-r--r--src/parse/pattern.cpp18
-rw-r--r--src/parse/types.cpp12
-rw-r--r--src/resolve/absolute.cpp7
11 files changed, 98 insertions, 171 deletions
diff --git a/src/ast/pattern.cpp b/src/ast/pattern.cpp
index 68d5d627..164ba962 100644
--- a/src/ast/pattern.cpp
+++ b/src/ast/pattern.cpp
@@ -32,6 +32,22 @@ namespace AST {
break;
}
),
+ (Float,
+ switch(e.type)
+ {
+ case CORETYPE_BOOL:
+ os << (e.value ? "true" : "false");
+ break;
+ case CORETYPE_ANY:
+ case CORETYPE_F32:
+ case CORETYPE_F64:
+ os << e.value;
+ break;
+ default:
+ BUG(Span(), "Hit integer in printing pattern literal");
+ break;
+ }
+ ),
(String,
os << "\"" << e << "\"";
),
@@ -109,63 +125,17 @@ namespace AST {
return os;
}
void operator%(Serialiser& s, Pattern::Value::Tag c) {
- s << Pattern::Value::tag_to_str(c);
}
void operator%(::Deserialiser& s, Pattern::Value::Tag& c) {
- ::std::string n;
- s.item(n);
- c = Pattern::Value::tag_from_str(n);
}
void operator%(::Serialiser& s, const Pattern::Value& v) {
- s % v.tag();
- TU_MATCH(Pattern::Value, (v), (e),
- (Invalid, ),
- (Integer,
- s % e.type;
- s.item( e.value );
- ),
- (String,
- s.item( e );
- ),
- (Named,
- s.item(e);
- )
- )
}
void operator%(::Deserialiser& s, Pattern::Value& v) {
- Pattern::Value::Tag tag;
- s % tag;
- switch(tag)
- {
- case Pattern::Value::TAGDEAD: throw "";
- case Pattern::Value::TAG_Invalid:
- v = Pattern::Value::make_Invalid({});
- break;
- case Pattern::Value::TAG_Integer: {
- enum eCoreType ct; s % ct;
- uint64_t val; s.item( val );
- v = Pattern::Value::make_Integer({ct, val});
- break; }
- case Pattern::Value::TAG_String: {
- ::std::string val;
- s.item( val );
- v = Pattern::Value::make_String(val);
- break; }
- case Pattern::Value::TAG_Named: {
- ::AST::Path val;
- s.item( val );
- v = Pattern::Value::make_Named(val);
- break; }
- }
}
void operator%(Serialiser& s, Pattern::Data::Tag c) {
- s << Pattern::Data::tag_to_str(c);
}
void operator%(::Deserialiser& s, Pattern::Data::Tag& c) {
- ::std::string n;
- s.item(n);
- c = Pattern::Data::tag_from_str(n);
}
Pattern::~Pattern()
@@ -193,6 +163,7 @@ AST::Pattern AST::Pattern::clone() const
TU_MATCH(::AST::Pattern::Value, (v), (e),
(Invalid, return Value(e);),
(Integer, return Value(e);),
+ (Float, return Value(e);),
(String, return Value(e);),
(Named, return Value::make_Named( AST::Path(e) );)
)
@@ -244,91 +215,7 @@ AST::Pattern AST::Pattern::clone() const
#define _D(VAR, ...) case Pattern::Data::TAG_##VAR: { m_data = Pattern::Data::make_##VAR({}); auto& ent = m_data.as_##VAR(); (void)&ent; __VA_ARGS__ } break;
SERIALISE_TYPE(Pattern::, "Pattern", {
- //s.item(m_binding);
- s % m_data.tag();
- TU_MATCH(Pattern::Data, (m_data), (e),
- (Any,
- ),
- (MaybeBind,
- ),
- (Macro,
- s.item( e.inv );
- ),
- (Box,
- s << e.sub;
- ),
- (Ref,
- s << e.mut;
- s << e.sub;
- ),
- (Value,
- s % e.start;
- s % e.end;
- ),
- (Tuple,
- s << e.sub_patterns;
- ),
- (WildcardStructTuple,
- s << e.path;
- ),
- (StructTuple,
- s << e.path;
- s << e.sub_patterns;
- ),
- (Struct,
- s << e.path;
- s << e.sub_patterns;
- ),
- (Slice,
- s << e.leading;
- s << e.extra_bind;
- s << e.trailing;
- )
- )
},{
- //s.item(m_binding);
- Pattern::Data::Tag tag;
- s % tag;
- switch(tag)
- {
- case Pattern::Data::TAGDEAD: throw "";
- _D(Any, )
- _D(MaybeBind,
- )
- _D(Macro,
- s.item( ent.inv );
- )
- _D(Box,
- s.item( ent.sub );
- )
- _D(Ref,
- s.item( ent.mut );
- s.item( ent.sub );
- )
- _D(Value,
- s % ent.start;
- s % ent.end;
- )
- _D(Tuple,
- s.item( ent.sub_patterns );
- )
- _D(WildcardStructTuple,
- s.item( ent.path );
- )
- _D(StructTuple,
- s.item( ent.path );
- s.item( ent.sub_patterns );
- )
- _D(Struct,
- s.item( ent.path );
- s.item( ent.sub_patterns );
- )
- _D(Slice,
- s.item( ent.leading );
- s.item( ent.extra_bind );
- s.item( ent.trailing );
- )
- }
});
}
diff --git a/src/ast/pattern.hpp b/src/ast/pattern.hpp
index fbdaa79e..7df25520 100644
--- a/src/ast/pattern.hpp
+++ b/src/ast/pattern.hpp
@@ -52,6 +52,10 @@ public:
enum eCoreType type;
uint64_t value; // Signed numbers are encoded as 2's complement
}),
+ (Float, struct {
+ enum eCoreType type;
+ double value;
+ }),
(String, ::std::string),
(Named, Path)
);
diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp
index 29711937..01917193 100644
--- a/src/hir/from_ast.cpp
+++ b/src/hir/from_ast.cpp
@@ -297,6 +297,13 @@ const ::HIR::SimplePath path_Sized = ::HIR::SimplePath("", {"marker", "Sized"});
e.value
});
),
+ (Float,
+ TODO(sp, "Floating point patterns");
+ //return ::HIR::Pattern::Value::make_Float({
+ // H::get_int_type(sp, e.type),
+ // e.value
+ // });
+ ),
(String,
return ::HIR::Pattern::Value::make_String(e);
),
diff --git a/src/main.cpp b/src/main.cpp
index b76e9b12..06c98064 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -8,6 +8,7 @@
#include <iostream>
#include <iomanip>
#include <string>
+#include <set>
#include "parse/lex.hpp"
#include "parse/parseerror.hpp"
#include "ast/ast.hpp"
@@ -25,17 +26,25 @@
int g_debug_indent_level = 0;
::std::string g_cur_phase;
-//::std::set< ::std::string> g_debug_disable_map;
+::std::set< ::std::string> g_debug_disable_map;
+void init_debug_list()
+{
+ //g_debug_disable_map.insert( "Parse" );
+ g_debug_disable_map.insert( "Expand" );
+ g_debug_disable_map.insert( "Resolve" );
+ g_debug_disable_map.insert( "Resolve UFCS paths" );
+ g_debug_disable_map.insert( "Typecheck Expressions" );
+}
bool debug_enabled()
{
- //return true;
- //return g_cur_phase != "Parse";
- //return g_cur_phase != "Parse" && g_cur_phase != "Expand";
- //return g_cur_phase != "Parse" && g_cur_phase != "Expand" && g_cur_phase != "Resolve";
- //return g_cur_phase != "Parse" && g_cur_phase != "Expand" && g_cur_phase != "Resolve" && g_cur_phase != "Resolve UFCS paths";
- return g_cur_phase != "Parse" && g_cur_phase != "Expand" && g_cur_phase != "Resolve" && g_cur_phase != "Resolve UFCS paths" && g_cur_phase != "Typecheck Expressions";
- //return false;
+ // TODO: Have an explicit enable list?
+ if( g_debug_disable_map.count(g_cur_phase) != 0 ) {
+ return false;
+ }
+ else {
+ return true;
+ }
//return g_cur_phase == "Lower MIR";
}
::std::ostream& debug_output(int indent, const char* function)
diff --git a/src/parse/common.hpp b/src/parse/common.hpp
index bf1c7448..484b3a67 100644
--- a/src/parse/common.hpp
+++ b/src/parse/common.hpp
@@ -41,7 +41,7 @@ extern AST::PathParams Parse_Path_GenericList(TokenStream& lex);
extern AST::MetaItem Parse_MetaItem(TokenStream& lex);
extern ::AST::MacroInvocation Parse_MacroInvocation(ProtoSpan ps, ::AST::MetaItems meta_items, ::std::string name, TokenStream& lex);
-extern TypeRef Parse_Type(TokenStream& lex, bool allow_trait_list = false);
+extern TypeRef Parse_Type(TokenStream& lex, bool allow_trait_list = true);
extern AST::Pattern Parse_Pattern(TokenStream& lex, bool is_refutable);
extern void Parse_Impl_Item(TokenStream& lex, AST::Impl& impl);
diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp
index fbc43dcf..44f0b35d 100644
--- a/src/parse/expr.cpp
+++ b/src/parse/expr.cpp
@@ -966,7 +966,7 @@ ExprNodeP Parse_ExprVal(TokenStream& lex)
// UFCS
case TOK_DOUBLE_LT:
- PUTBACK(tok, lex);
+ PUTBACK(TOK_LT, lex);
case TOK_LT: {
TypeRef ty = Parse_Type(lex);
if( GET_TOK(tok, lex) == TOK_RWORD_AS ) {
diff --git a/src/parse/lex.cpp b/src/parse/lex.cpp
index 2b84d0e4..c3bfa5e5 100644
--- a/src/parse/lex.cpp
+++ b/src/parse/lex.cpp
@@ -20,8 +20,8 @@
#include <typeinfo>
#include <algorithm> // std::count
-const bool DEBUG_PRINT_TOKENS = false;
-//const bool DEBUG_PRINT_TOKENS = true;
+//const bool DEBUG_PRINT_TOKENS = false;
+const bool DEBUG_PRINT_TOKENS = true;
Lexer::Lexer(const ::std::string& filename):
m_path(filename.c_str()),
@@ -207,7 +207,12 @@ signed int Lexer::getSymbol()
while( chars[ofs] && chars[ofs] == ch )
{
- ch = this->getc();
+ try {
+ ch = this->getc();
+ }
+ catch(Lexer::EndOfFile) {
+ ch = 0;
+ }
ofs ++;
}
if( chars[ofs] == 0 )
@@ -322,7 +327,9 @@ Token Lexer::getTokenInt()
DEC,
HEX,
} num_mode = DEC;
- // TODO: handle integers/floats
+
+
+ // Handle integers/floats
uint64_t val = 0;
if( ch == '0' ) {
// Octal/hex handling
@@ -399,12 +406,24 @@ Token Lexer::getTokenInt()
}
return Token(val, CORETYPE_ANY);
}
- // Single dot
+ // Single dot - Still a float. (TODO)
else if( !isdigit(ch) )
{
this->ungetc();
- this->m_next_token = Token(TOK_DOT);
- return Token(val, CORETYPE_ANY);
+ if( issym(ch) )
+ {
+ this->m_next_token = Token(TOK_DOT);
+ return Token(val, CORETYPE_ANY);
+ }
+ else
+ {
+ double fval = static_cast<double>(val);
+ return Token(fval, CORETYPE_ANY);
+ }
+ }
+ else
+ {
+ // Digit, continue
}
}
@@ -778,6 +797,8 @@ double Lexer::parseFloat(uint64_t whole)
this->ungetc();
buf[ofs] = 0;
+ DEBUG("buf = " << buf << ", ch = '" << ch << "'");
+
return ::std::strtod(buf, NULL);
}
diff --git a/src/parse/paths.cpp b/src/parse/paths.cpp
index 6b84ec0b..ecfdf7dd 100644
--- a/src/parse/paths.cpp
+++ b/src/parse/paths.cpp
@@ -16,6 +16,8 @@ AST::PathParams Parse_Path_GenericList(TokenStream& lex);
AST::Path Parse_Path(TokenStream& lex, eParsePathGenericMode generic_mode)
{
+ TRACE_FUNCTION_F("generic_mode="<<generic_mode);
+
Token tok;
switch( GET_TOK(tok, lex) )
{
@@ -43,7 +45,7 @@ AST::Path Parse_Path(TokenStream& lex, eParsePathGenericMode generic_mode)
case TOK_DOUBLE_LT:
lex.putback( Token(TOK_LT) );
case TOK_LT: {
- TypeRef ty = Parse_Type(lex);
+ TypeRef ty = Parse_Type(lex, false); // Don't allow un-parenthesied trait objects
if( GET_TOK(tok, lex) == TOK_RWORD_AS ) {
::AST::Path trait;
if( GET_TOK(tok, lex) == TOK_DOUBLE_COLON ) {
@@ -100,6 +102,8 @@ AST::Path Parse_Path(TokenStream& lex, bool is_abs, eParsePathGenericMode generi
::std::vector<AST::PathNode> Parse_PathNodes(TokenStream& lex, eParsePathGenericMode generic_mode)
{
+ TRACE_FUNCTION_F("generic_mode="<<generic_mode);
+
Token tok;
::std::vector<AST::PathNode> ret;
@@ -145,7 +149,7 @@ AST::Path Parse_Path(TokenStream& lex, bool is_abs, eParsePathGenericMode generi
TypeRef ret_type = TypeRef( TypeRef::TagUnit(), Span(tok.get_pos()) );
if( GET_TOK(tok, lex) == TOK_THINARROW ) {
- ret_type = Parse_Type(lex);
+ ret_type = Parse_Type(lex, false);
}
else {
PUTBACK(tok, lex);
@@ -217,7 +221,7 @@ AST::Path Parse_Path(TokenStream& lex, bool is_abs, eParsePathGenericMode generi
{
::std::string name = tok.str();
GET_CHECK_TOK(tok, lex, TOK_EQUAL);
- assoc_bounds.push_back( ::std::make_pair( mv$(name), Parse_Type(lex) ) );
+ assoc_bounds.push_back( ::std::make_pair( mv$(name), Parse_Type(lex,false) ) );
break;
}
default:
diff --git a/src/parse/pattern.cpp b/src/parse/pattern.cpp
index 424099ff..87f08d31 100644
--- a/src/parse/pattern.cpp
+++ b/src/parse/pattern.cpp
@@ -206,19 +206,19 @@ AST::Pattern Parse_PatternReal1(TokenStream& lex, bool is_refutable)
dt = CORETYPE_I32;
return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({dt, -tok.intval()}) );
}
- //else if( tok.type() == TOK_FLOAT )
- //{
- // auto dt = tok.datatype();
- // if(dt == CORETYPE_ANY)
- // dt = CORETYPE_F32;
- // return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({dt, reinterpret_cast<uint64_t>(-tok.floatval()), dt}) );
- //}
+ else if( tok.type() == TOK_FLOAT )
+ {
+ auto dt = tok.datatype();
+ if(dt == CORETYPE_ANY)
+ dt = CORETYPE_F32;
+ return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Float({dt, -tok.floatval()}) );
+ }
else
{
throw ParseError::Unexpected(lex, tok, {TOK_INTEGER, TOK_FLOAT});
}
- //case TOK_FLOAT:
- // return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({tok.datatype(), reinterpret_cast<uint64_t>(tok.floatval())}) );
+ case TOK_FLOAT:
+ return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Float({tok.datatype(), tok.floatval()}) );
case TOK_INTEGER:
return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({tok.datatype(), tok.intval()}) );
case TOK_RWORD_TRUE:
diff --git a/src/parse/types.cpp b/src/parse/types.cpp
index 6f3a3d58..34afc484 100644
--- a/src/parse/types.cpp
+++ b/src/parse/types.cpp
@@ -78,7 +78,7 @@ TypeRef Parse_Type_Int(TokenStream& lex, bool allow_trait_list)
// TODO: Handle HRLS in fn types
return Parse_Type_Fn(lex, hrls);
default:
- return Parse_Type_Path(lex, hrls, allow_trait_list);
+ return Parse_Type_Path(lex, hrls, true);
}
}
// <ident> - Either a primitive, or a path
@@ -114,12 +114,12 @@ TypeRef Parse_Type_Int(TokenStream& lex, bool allow_trait_list)
}
if( tok.type() == TOK_RWORD_MUT ) {
// Mutable reference
- return TypeRef(TypeRef::TagReference(), lex.end_span(ps), true, Parse_Type(lex));
+ return TypeRef(TypeRef::TagReference(), lex.end_span(ps), true, Parse_Type(lex, false));
}
else {
PUTBACK(tok, lex);
// Immutable reference
- return TypeRef(TypeRef::TagReference(), lex.end_span(ps), false, Parse_Type(lex));
+ return TypeRef(TypeRef::TagReference(), lex.end_span(ps), false, Parse_Type(lex, false));
}
throw ParseError::BugCheck("Reached end of Parse_Type:AMP");
}
@@ -130,10 +130,10 @@ TypeRef Parse_Type_Int(TokenStream& lex, bool allow_trait_list)
{
case TOK_RWORD_MUT:
// Mutable pointer
- return TypeRef(TypeRef::TagPointer(), lex.end_span(ps), true, Parse_Type(lex));
+ return TypeRef(TypeRef::TagPointer(), lex.end_span(ps), true, Parse_Type(lex, false));
case TOK_RWORD_CONST:
// Immutable pointer
- return TypeRef(TypeRef::TagPointer(), lex.end_span(ps), false, Parse_Type(lex));
+ return TypeRef(TypeRef::TagPointer(), lex.end_span(ps), false, Parse_Type(lex, false));
default:
throw ParseError::Unexpected(lex, tok, {TOK_RWORD_CONST, TOK_RWORD_MUT});
}
@@ -247,7 +247,7 @@ TypeRef Parse_Type_Fn(TokenStream& lex, ::std::vector<::std::string> hrls)
TypeRef ret_type = TypeRef(TypeRef::TagUnit(), Span(tok.get_pos()));
if( GET_TOK(tok, lex) == TOK_THINARROW )
{
- ret_type = Parse_Type(lex);
+ ret_type = Parse_Type(lex, false);
}
else {
PUTBACK(tok, lex);
diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp
index abfaa672..f2e58703 100644
--- a/src/resolve/absolute.cpp
+++ b/src/resolve/absolute.cpp
@@ -1136,14 +1136,9 @@ void Resolve_Absolute_Generic(Context& context, ::AST::GenericParams& params)
// Locals shouldn't be possible, as they'd end up as MaybeBind. Will assert the path class.
void Resolve_Absolute_PatternValue(/*const*/ Context& context, const Span& sp, ::AST::Pattern::Value& val)
{
- TU_MATCH(::AST::Pattern::Value, (val), (e),
- (Invalid, ),
- (Integer, ),
- (String, ),
- (Named,
+ TU_IFLET(::AST::Pattern::Value, val, Named, e,
//assert( ! e.is_trivial() );
Resolve_Absolute_Path(context, sp, Context::LookupMode::Constant, e);
- )
)
}
void Resolve_Absolute_Pattern(Context& context, bool allow_refutable, ::AST::Pattern& pat)