Skip to content

Commit

Permalink
fixed bug that lead to char literal being converted to ruby string
Browse files Browse the repository at this point in the history
  • Loading branch information
v0dro committed May 22, 2018
1 parent dd9065f commit f715e34
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
17 changes: 13 additions & 4 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -788,14 +788,23 @@ end

# Handling Strings

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.
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.

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"`.
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"`.

# Differences from C

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

# Differences from Ruby

Expand Down
2 changes: 1 addition & 1 deletion lib/rubex/ast/expression/binary/binary_boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Expression
class BinaryBoolean < Binary
def analyse_types(local_scope)
@left.analyse_types local_scope
@right.analyse_types local_scope
@right.analyse_for_target_type @left.type, local_scope
if type_of(@left).object? || type_of(@right).object?

@left = @left.to_ruby_object
Expand Down
4 changes: 2 additions & 2 deletions spec/c_struct_interface_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
end
end

context ".compile", hell: true do
context ".compile" do
it "generates valid C code" do
t, c, e, h = Rubex::Compiler.compile @path + ".rubex", test: true
end
end

context "Black Box testing", hell: true do
context "Black Box testing" do
it "compiles and checks for valid output" do
setup_and_teardown_compiled_files(test_case) do |dir|
require_relative "#{dir}/#{test_case}.#{os_extension}"
Expand Down
10 changes: 10 additions & 0 deletions spec/fixtures/string_literals/string_literals.rubex
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ end
def string_ret
return "This is a returned string."
end

def char_literal
char *s = "hello world"

if (s[0] == 'h')
return true
else
return false
end
end
5 changes: 3 additions & 2 deletions spec/string_literals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@
end
end

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

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

expect(strings).to eq("Ted says \"Oh my God thats a big sandwich!\"")
expect(string_ret).to eq("This is a returned string.")
expect(char_literal).to eq(true)
end
end
end
Expand Down

0 comments on commit f715e34

Please sign in to comment.