Skip to content

Commit f715e34

Browse files
committed
fixed bug that lead to char literal being converted to ruby string
1 parent dd9065f commit f715e34

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

REFERENCE.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -788,14 +788,23 @@ end
788788

789789
# Handling Strings
790790

791-
For purposes of optimization and compatibility with C, Rubex makes certain assumptions about strings. When you assign a Ruby object to a `char*`, Rubex will automatically pass a pointer to the C string contained inside the object to the `char*`, thereby increasing the efficiency of operations on the string. The resulting string is a regular `\0` delimited C string.
791+
For purposes of optimization and compatibility with C, Rubex makes certain
792+
assumptions about strings. When you assign a Ruby object to a `char*`, Rubex
793+
will automatically pass a pointer to the C string contained inside the object
794+
to the `char*`, thereby increasing the efficiency of operations on the string.
795+
The resulting string is a regular `\0` delimited C string.
792796

793-
It should be noted that strings MUST use the double quotation syntax (`""`) and not single quotes in all cases. Single quote is reserved for use by C `char` data. For example, to assign a literal value to a C char, you can write `char a = 'b'`, to assign a literal C string to a `char*` array, use `char* a = "hello"`.
797+
It should be noted that strings MUST use the double quotation syntax (`""`) and
798+
not single quotes in all cases. Single quote is reserved for use by C `char` data.
799+
For example, to assign a literal value to a C char, you can write `char a = 'b'`,
800+
to assign a literal C string to a `char*` array, use `char* a = "hello"`.
794801

795802
# Differences from C
796803

797-
* Rubex does not have dereferencing operator (`*`). Instead use `[0]` to access values pointed to by pointers.
798-
* There is no `->` operator for accessing struct elements from a pointer to a struct. Use the `.` operator directly.
804+
* Rubex does not have dereferencing operator (`*`). Instead use `[0]` to access
805+
values pointed to by pointers.
806+
* There is no `->` operator for accessing struct elements from a pointer to a struct.
807+
Use the `.` operator directly.
799808

800809
# Differences from Ruby
801810

lib/rubex/ast/expression/binary/binary_boolean.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Expression
44
class BinaryBoolean < Binary
55
def analyse_types(local_scope)
66
@left.analyse_types local_scope
7-
@right.analyse_types local_scope
7+
@right.analyse_for_target_type @left.type, local_scope
88
if type_of(@left).object? || type_of(@right).object?
99

1010
@left = @left.to_ruby_object

spec/c_struct_interface_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
end
1616
end
1717

18-
context ".compile", hell: true do
18+
context ".compile" do
1919
it "generates valid C code" do
2020
t, c, e, h = Rubex::Compiler.compile @path + ".rubex", test: true
2121
end
2222
end
2323

24-
context "Black Box testing", hell: true do
24+
context "Black Box testing" do
2525
it "compiles and checks for valid output" do
2626
setup_and_teardown_compiled_files(test_case) do |dir|
2727
require_relative "#{dir}/#{test_case}.#{os_extension}"

spec/fixtures/string_literals/string_literals.rubex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ end
1313
def string_ret
1414
return "This is a returned string."
1515
end
16+
17+
def char_literal
18+
char *s = "hello world"
19+
20+
if (s[0] == 'h')
21+
return true
22+
else
23+
return false
24+
end
25+
end

spec/string_literals_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@
1414
end
1515
end
1616

17-
context ".compile" do
17+
context ".compile", hell: true do
1818
it "compiles to C." do
1919
t, c, e = Rubex::Compiler.compile(@path + '.rubex', test: true)
2020
end
2121
end
2222

23-
context "Black Box testing" do
23+
context "Black Box testing", hell: true do
2424
it "compiles and checks for valid output" do
2525
setup_and_teardown_compiled_files(test_case) do |dir|
2626
require_relative "#{dir}/#{test_case}.#{os_extension}"
2727

2828
expect(strings).to eq("Ted says \"Oh my God thats a big sandwich!\"")
2929
expect(string_ret).to eq("This is a returned string.")
30+
expect(char_literal).to eq(true)
3031
end
3132
end
3233
end

0 commit comments

Comments
 (0)