From 2364009fd9b1a49c31b035220b3a0f53e4988f85 Mon Sep 17 00:00:00 2001 From: Tad Hardesty Date: Tue, 9 Apr 2019 22:36:14 -0700 Subject: [PATCH] Add additional Type to allow is_truthy --- src/dreamchecker/lib.rs | 47 ++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/dreamchecker/lib.rs b/src/dreamchecker/lib.rs index ac0983f1..bbc46c65 100644 --- a/src/dreamchecker/lib.rs +++ b/src/dreamchecker/lib.rs @@ -40,8 +40,10 @@ enum Type<'o> { // Primitives ------------------------------------------------------------- Null, // Only thing that isnull(). - String, // Only thing that istext(). - Number, // Only thing that isnum(). + EmptyString, + String, + Zero, + Number, Resource, // Only thing that isfile(), file() returns this. // Types ------------------------------------------------------------------ @@ -53,6 +55,7 @@ enum Type<'o> { // These are not nameable and have no isX() proc, but are "something". Global, AbstractFilter, + ProcPath, } impl<'o> Type<'o> { @@ -71,8 +74,34 @@ impl<'o> Type<'o> { ConstFn::Sound => Type::Instance(objtree.expect("/sound")), ConstFn::Filter => Type::AbstractFilter, }, - // TODO: New => Instance, Prefab => Typepath - _ => Type::Any, + Constant::New { type_, args: _ } => { + if let Some(pop) = type_.as_ref() { + if let Some(ty) = objtree.type_by_path(&pop.path) { + Type::Instance(ty) + } else { + Type::Any + } + } else { + // TODO: receive type hint here + Type::Any + } + }, + Constant::Prefab(pop) => { + if let Some(ty) = objtree.type_by_path(&pop.path) { + Type::Instance(ty) + } else { + Type::Any + } + }, + } + } + + fn is_truthy(&self) -> bool { + match self { + Type::Null => false, + Type::EmptyString => false, + Type::Zero => false, + _ => true, } } @@ -86,6 +115,7 @@ impl<'o> Type<'o> { fn istext(&self) -> bool { match self { Type::String => true, + Type::EmptyString => true, _ => false, } } @@ -93,6 +123,7 @@ impl<'o> Type<'o> { fn isnum(&self) -> bool { match self { Type::Number => true, + Type::Zero => true, _ => false, } } @@ -148,14 +179,6 @@ impl<'o> Type<'o> { } } - fn isicon(&self) -> Option { - match self { - Type::Instance(ty) => Some(ty.is_subtype_of(&ty.tree().expect("/icon"))), - Type::Resource => None, // Don't know whether file is ".dmi". - _ => Some(false), - } - } - fn istype(&self, other: TypeRef<'o>) -> bool { // The "tricky" bits above do not apply here. match self {