type.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "std.h"
  2. #include "type.h"
  3. static struct v_type : public Type{
  4. bool canCastTo( Type *t ){
  5. return t==Type::void_type;
  6. }
  7. }v;
  8. static struct i_type : public Type{
  9. bool intType(){
  10. return true;
  11. }
  12. bool canCastTo( Type *t ){
  13. return t==Type::int_type || t==Type::float_type || t==Type::string_type;
  14. }
  15. }i;
  16. static struct f_type : public Type{
  17. bool floatType(){
  18. return true;
  19. }
  20. bool canCastTo( Type *t ){
  21. return t==Type::int_type || t==Type::float_type || t==Type::string_type;
  22. }
  23. }f;
  24. static struct s_type : public Type{
  25. bool stringType(){
  26. return true;
  27. }
  28. bool canCastTo( Type *t ){
  29. return t==Type::int_type || t==Type::float_type || t==Type::string_type;
  30. }
  31. }s;
  32. bool StructType::canCastTo( Type *t ){
  33. return t==this || t==Type::null_type || (this==Type::null_type && t->structType());
  34. }
  35. bool VectorType::canCastTo( Type *t ){
  36. if( this==t ) return true;
  37. if( VectorType *v=t->vectorType() ){
  38. if( elementType!=v->elementType ) return false;
  39. if( sizes.size()!=v->sizes.size() ) return false;
  40. for( int k=0;k<sizes.size();++k ){
  41. if( sizes[k]!=v->sizes[k] ) return false;
  42. }
  43. return true;
  44. }
  45. return false;
  46. }
  47. static StructType n( "Null" );
  48. Type *Type::void_type=&v;
  49. Type *Type::int_type=&i;
  50. Type *Type::float_type=&f;
  51. Type *Type::string_type=&s;
  52. Type *Type::null_type=&n;