lib/kompiler/parsers.rb in kompiler-0.2.0.pre.1 vs lib/kompiler/parsers.rb in kompiler-0.2.0.pre.2
- old
+ new
@@ -20,10 +20,12 @@
str_content << "\n"
elsif code[i] == "r"
str_content << "\r"
elsif code[i] == "\\"
str_content << "\\"
+ elsif code[i] == "0"
+ str_content << "\0"
else
str_content << "\\"
str_content << code[i]
end
next_char_backslashed = false
@@ -143,19 +145,45 @@
return [true, int]
end
+def self.check_char_operand(str)
+
+ # If doesn't start with ' or doesn't end with ', return false
+ if (str[0] != "'") || (str[-1] != "'")
+ return [false, nil]
+ end
+
+ # Use existing logic to parse the contents
+ to_parse = str.dup
+ to_parse[0] = '"'
+ to_parse[-1] = '"'
+ content, parse_i = parse_str(to_parse)
+
+ # If more than one character, return false
+ if content.size != 1
+ return [false, nil]
+ end
+
+ return [true, content.encode("ascii").bytes[0]]
+end
+
+
+
def self.check_immediate_operand(operand_str)
is_bin, bin_value = check_binary_operand(operand_str)
return [true, {type: "immediate", value: bin_value, def_type: "binary"}] if is_bin
is_decimal, decimal_value = check_decimal_operand(operand_str)
return [true, {type: "immediate", value: decimal_value, def_type: "decimal"}] if is_decimal
is_hex, hex_value = check_hex_operand(operand_str)
return [true, {type: "immediate", value: hex_value, def_type: "hex"}] if is_hex
+
+ is_char, char_value = check_char_operand(operand_str)
+ return [true, {type: "immediate", value: char_value, def_type: "char"}] if is_char
return [false, nil]
end
\ No newline at end of file