better typing
This commit is contained in:
parent
b79c3e0f5a
commit
27a6d0eb2d
1 changed files with 78 additions and 38 deletions
100
05/parse.b
100
05/parse.b
|
@ -118,15 +118,46 @@ function parse_expression
|
||||||
if c == SYMBOL_LSHIFT goto type_binary_left_promote
|
if c == SYMBOL_LSHIFT goto type_binary_left_promote
|
||||||
if c == SYMBOL_RSHIFT goto type_binary_left_promote
|
if c == SYMBOL_RSHIFT goto type_binary_left_promote
|
||||||
if c == SYMBOL_LSQUARE goto type_subscript
|
if c == SYMBOL_LSQUARE goto type_subscript
|
||||||
if c == SYMBOL_EQ_EQ goto type_binary_comparison
|
if c == SYMBOL_EQ_EQ goto type_binary_int
|
||||||
if c == SYMBOL_NOT_EQ goto type_binary_comparison
|
if c == SYMBOL_NOT_EQ goto type_binary_int
|
||||||
if c == SYMBOL_LT_EQ goto type_binary_comparison
|
if c == SYMBOL_LT_EQ goto type_binary_int
|
||||||
if c == SYMBOL_GT_EQ goto type_binary_comparison
|
if c == SYMBOL_GT_EQ goto type_binary_int
|
||||||
if c == SYMBOL_LT goto type_binary_comparison
|
if c == SYMBOL_LT goto type_binary_int
|
||||||
if c == SYMBOL_GT goto type_binary_comparison
|
if c == SYMBOL_GT goto type_binary_int
|
||||||
if c < SYMBOL_EQ goto type_binary_usual
|
if c == SYMBOL_COMMA goto type_binary_right
|
||||||
if c > SYMBOL_OR_EQ goto type_binary_usual
|
if c == SYMBOL_EQ goto type_binary_left
|
||||||
goto type_binary_left
|
if c == SYMBOL_PLUS_EQ goto type_binary_left
|
||||||
|
if c == SYMBOL_MINUS_EQ goto type_binary_left
|
||||||
|
if c == SYMBOL_TIMES_EQ goto type_binary_left
|
||||||
|
if c == SYMBOL_DIV_EQ goto type_binary_left
|
||||||
|
if c == SYMBOL_PERCENT_EQ goto type_binary_left
|
||||||
|
if c == SYMBOL_AND_EQ goto type_binary_left_int
|
||||||
|
if c == SYMBOL_XOR_EQ goto type_binary_left_int
|
||||||
|
if c == SYMBOL_OR_EQ goto type_binary_left_int
|
||||||
|
if c == SYMBOL_LSHIFT_EQ goto type_binary_left_int
|
||||||
|
if c == SYMBOL_RSHIFT_EQ goto type_binary_left_int
|
||||||
|
if c == SYMBOL_OR_OR goto type_binary_int
|
||||||
|
if c == SYMBOL_AND_AND goto type_binary_int
|
||||||
|
if c == SYMBOL_AND goto type_binary_usual_int
|
||||||
|
if c == SYMBOL_XOR goto type_binary_usual_int
|
||||||
|
if c == SYMBOL_OR goto type_binary_usual_int
|
||||||
|
if c == SYMBOL_PLUS goto type_plus
|
||||||
|
if c == SYMBOL_MINUS goto type_minus
|
||||||
|
if c == SYMBOL_TIMES goto type_binary_usual
|
||||||
|
if c == SYMBOL_DIV goto type_binary_usual
|
||||||
|
if c == SYMBOL_PERCENT goto type_binary_usual_int
|
||||||
|
|
||||||
|
fputs(2, .str_binop_this_shouldnt_happen)
|
||||||
|
exit(1)
|
||||||
|
:str_binop_this_shouldnt_happen
|
||||||
|
string Bad binop symbol (this shouldn't happen).
|
||||||
|
byte 10
|
||||||
|
byte 0
|
||||||
|
|
||||||
|
:type_plus
|
||||||
|
byte 0xcc ; @TODO
|
||||||
|
:type_minus
|
||||||
|
byte 0xcc ; @TODO
|
||||||
:type_subscript
|
:type_subscript
|
||||||
p = types + *4a
|
p = types + *4a
|
||||||
if *1p == TYPE_POINTER goto type_subscript_pointer
|
if *1p == TYPE_POINTER goto type_subscript_pointer
|
||||||
|
@ -146,17 +177,29 @@ function parse_expression
|
||||||
:type_binary_usual
|
:type_binary_usual
|
||||||
*4type = expr_binary_type_usual_conversions(tokens, *4a, *4b)
|
*4type = expr_binary_type_usual_conversions(tokens, *4a, *4b)
|
||||||
return out
|
return out
|
||||||
:type_binary_comparison
|
:type_binary_usual_int
|
||||||
|
*4type = expr_binary_type_usual_conversions(tokens, *4a, *4b)
|
||||||
|
p = types + *4type
|
||||||
|
if *1p >= TYPE_FLOAT goto expr_binary_bad_types
|
||||||
|
return out
|
||||||
|
:type_binary_int
|
||||||
*4type = TYPE_INT
|
*4type = TYPE_INT
|
||||||
return out
|
return out
|
||||||
|
:type_binary_left_int
|
||||||
|
p = types + *4a
|
||||||
|
if *1p >= TYPE_FLOAT goto expr_binary_bad_types
|
||||||
|
goto type_binary_left
|
||||||
:type_binary_left
|
:type_binary_left
|
||||||
*4type = *4a
|
*4type = *4a
|
||||||
return out
|
return out
|
||||||
|
:type_binary_right
|
||||||
|
*4type = *4b
|
||||||
|
return out
|
||||||
:type_binary_left_promote
|
:type_binary_left_promote
|
||||||
*4type = type_promotion(*4a)
|
*4type = type_promotion(*4a)
|
||||||
return out
|
return out
|
||||||
|
:expr_binary_bad_types
|
||||||
return out
|
bad_types_to_operator(tokens, *4a, *4b)
|
||||||
;@TODO: casts
|
;@TODO: casts
|
||||||
|
|
||||||
|
|
||||||
|
@ -300,6 +343,7 @@ function parse_expression
|
||||||
:return_type_double
|
:return_type_double
|
||||||
return TYPE_DOUBLE
|
return TYPE_DOUBLE
|
||||||
|
|
||||||
|
; the "usual conversions" for binary operators, as the C standard calls it
|
||||||
function expr_binary_type_usual_conversions
|
function expr_binary_type_usual_conversions
|
||||||
argument token ; for errors
|
argument token ; for errors
|
||||||
argument type1
|
argument type1
|
||||||
|
@ -310,17 +354,12 @@ function expr_binary_type_usual_conversions
|
||||||
|
|
||||||
if type1 == 0 goto return_0
|
if type1 == 0 goto return_0
|
||||||
if type2 == 0 goto return_0
|
if type2 == 0 goto return_0
|
||||||
; @TODO: pointer types
|
|
||||||
ptype1 = types + type1
|
ptype1 = types + type1
|
||||||
ptype2 = types + type2
|
ptype2 = types + type2
|
||||||
|
|
||||||
type1 = *1ptype1
|
if type1 > TYPE_DOUBLE goto usual_bad_types_to_operator
|
||||||
type2 = *1ptype2
|
if type2 > TYPE_DOUBLE goto usual_bad_types_to_operator
|
||||||
if type1 == TYPE_POINTER goto type1_pointer
|
|
||||||
if type2 == TYPE_POINTER goto type2_pointer
|
|
||||||
|
|
||||||
if type1 > TYPE_DOUBLE goto bad_types_to_operator
|
|
||||||
if type2 > TYPE_DOUBLE goto bad_types_to_operator
|
|
||||||
|
|
||||||
; "if either operand has type double, the other operand is converted to double"
|
; "if either operand has type double, the other operand is converted to double"
|
||||||
if type1 == TYPE_DOUBLE goto return_type_double
|
if type1 == TYPE_DOUBLE goto return_type_double
|
||||||
|
@ -340,13 +379,18 @@ function expr_binary_type_usual_conversions
|
||||||
; "Otherwise, both operands have type int."
|
; "Otherwise, both operands have type int."
|
||||||
goto return_type_int
|
goto return_type_int
|
||||||
|
|
||||||
:type1_pointer
|
:str_space_and_space
|
||||||
if type2 == TYPE_POINTER goto return_type_long ; this must be a pointer difference
|
string and
|
||||||
return ptype1 - types ; e.g. p_int + 5
|
byte 32
|
||||||
:type2_pointer
|
byte 0
|
||||||
return ptype2 - types ; e.g. 5 + p_int
|
:usual_bad_types_to_operator
|
||||||
|
bad_types_to_operator(token, type1, type2)
|
||||||
|
|
||||||
|
function bad_types_to_operator
|
||||||
|
argument token
|
||||||
|
argument type1
|
||||||
|
argument type2
|
||||||
|
|
||||||
:bad_types_to_operator
|
|
||||||
fprint_token_location(2, token)
|
fprint_token_location(2, token)
|
||||||
fputs(2, .str_bad_types_to_operator)
|
fputs(2, .str_bad_types_to_operator)
|
||||||
print_type(type1)
|
print_type(type1)
|
||||||
|
@ -358,10 +402,6 @@ function expr_binary_type_usual_conversions
|
||||||
string : Bad types to operator:
|
string : Bad types to operator:
|
||||||
byte 32
|
byte 32
|
||||||
byte 0
|
byte 0
|
||||||
:str_space_and_space
|
|
||||||
string and
|
|
||||||
byte 32
|
|
||||||
byte 0
|
|
||||||
|
|
||||||
function type_promotion
|
function type_promotion
|
||||||
argument type
|
argument type
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue