Skip to content

Commit

Permalink
Add _cstring literal suffix and document
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Sep 7, 2020
1 parent 8819f02 commit a6aa52d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ <h2>What is Nelua?</h2>
<p>
Nelua takes advantage of ahead of time compilation
using powerful optimized C compilers such as GCC or Clang thus generating very
efficient native code, it doesn't use a interpreter at runtime.
efficient native code, it doesn't use an interpreter at runtime.
</p>
</div>
<div class="col">
Expand Down
6 changes: 4 additions & 2 deletions docs/pages/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,9 @@ Number literals are defined similar as in Lua:
local a = 1234 -- variable of type 'integer'
local b = 0xff -- variable of type 'integer'
local c = 3.14159 -- variable of type 'number'
local d: integer
print(a,b,c,d) -- outputs: 1234 255 3.141590 0
local d = 'A'_uint8 -- variable of type 'uint8' set from an ASCII character
local e: integer
print(a,b,c,d,e) -- outputs: 1234 255 3.141590 65 0
```

The `integer` is the default type for integral literals without suffix.
Expand Down Expand Up @@ -2315,6 +2316,7 @@ For importing C functions, additional compatibility primitive types are provided
| `culonglong` | `unsigned long long` | `_culonglong` |
| `csize` | `size_t` | `_csize` |
| `clongdouble` | `long double` | `_clongdouble` |
| `cstring` | `char*` | `_cstring` |
{: .table.table-bordered.table-striped.table-sm}

Use this types for **importing C functions only**, for doing usual
Expand Down
12 changes: 8 additions & 4 deletions nelua/analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ function visitors.String(_, node)
if not type then
node:raisef("literal suffix '%s' is undefined for strings", literal)
end
if #value ~= 1 then
node:raisef("literal suffix '%s' expects a string of length 1")
if type.is_arithmetic then
if #value ~= 1 then
node:raisef("literal suffix '%s' expects a string of length 1")
end
attr.type = type
value = bn.new(string.byte(value))
elseif type.is_cstring then
attr.type = primtypes.cstring
end
attr.type = type
value = bn.new(string.byte(value))
else
if node.desiredtype and node.desiredtype.is_cstring then
attr.type = primtypes.cstring
Expand Down
1 change: 1 addition & 0 deletions nelua/typedefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ typedefs.string_literals_types = {
_cchar = primtypes.cchar,
_cuchar = primtypes.cuchar,
_cschar = primtypes.cschar,
_cstring = primtypes.cstring,
}

-- Ordered list of signed types for performing type promotion.
Expand Down
1 change: 1 addition & 0 deletions spec/05-cgenerator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ it("number literals", function()
assert.generate_c("local a = ' '_cchar", "char a = ' ';")
assert.generate_c("local a = ' '_cschar", "signed char a = 32;")
assert.generate_c("local a = ' '_cuchar", "unsigned char a = 32U;")
assert.generate_c("local a = 'str'_cstring", 'char* a = __strlit')
end)

it("type cast", function()
Expand Down

0 comments on commit a6aa52d

Please sign in to comment.