Comments (3)
Nelua processor is very different from other programming languages preprocessors in a unique way, it does not work at syntax and token level, but at AST (abstract syntax tree) level and with type inference. This has unique advantages like being able to introspect AST nodes and variables while in the preprocessor context, not many languages can do this. But there are drawbacks, like you cannot insert any set of tokens between preprocessor directives. This is specially true for ifelse blocks and switches, you cannot break them apart between preprocessor directives, the code between the directives must be a complete and valid AST node.
So your last example, is not fine:
if false then
print 'a'
## if true then
else
print 'b'
## end
end
This is how the compiler understands, luckily it will compile, but it was not doing what you expected.
Given this limitation, you have two choices, one is to use multiple if conditions like you suggested, and it's fine to do this, because switches are converted to ifs most of the time anyway when compiling. The second, if you really want to emit a switch, is to generate AST by hand, like the following example:
local Weekdays = @enum{
Sunday = 0,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
-- way 1
function Weekdays.show1(wd: Weekdays): string
##[[
local cases = {}
for _, field in ipairs(Weekdays.value.fields) do
table.insert(cases, {aster.Number{tostring(field.value)}})
table.insert(cases, aster.Block{aster.Return{aster.String{'Weekdays.'..field.name}}})
end
inject_statement(aster.Switch{aster.Id{'wd'}, cases})
]]
assert(false, 'invalid weekday')
return ''
end
print(Weekdays.Sunday:show1())
But this way you have to manually emit AST nodes by hand, after reading astdefs.lua
to understand the AST schema. This can be more difficult but also more powerful, because you can generate any kind of code.
But what if you want to mix manual AST node creation with usual code, thinking of this, in commit cd6986b I introduced wrap_statement
macro, so there is a new way:
local Weekdays = @enum{
Sunday = 0,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
-- way 2
function Weekdays.show2(wd: Weekdays): string
## local cases = {}
## for _, field in ipairs(Weekdays.value.fields) do
## table.insert(cases, {aster.Number{tostring(field.value)}})
## table.insert(cases, wrap_statement(function()
-- back to normal code context
return #['Weekdays.'..field.name]#
## end))
## end
## inject_statement(aster.Switch{aster.Id{'wd'}, cases})
assert(false, 'invalid weekday')
return ''
end
print(Weekdays.Saturday:show2())
from nelua-lang.
...so, remind me when are you going to write that Compiler book? /jk 🤣 I honestly wish I had the financial resources to fund you in any project you would like to invest your time and effort, because I learn so many things from you.
from nelua-lang.
At first, I thought it was the missing do
from switch wd
, but it seems switch
is not supported with preprocessor's parsing yet.
from nelua-lang.
Related Issues (20)
- (@span(byte)){...} ordered field cast permits invalid, rejects valid construction HOT 1
- Executables reflect compile-time isatty conditions HOT 3
- <comptime> record parameters can't be provided; fields can't be used in runtime code HOT 1
- Copyright timespan needs update HOT 3
- Invalid unitname generated for directories that look like numeric literals HOT 1
- No line number in error if a hashmap index has a type error
- Range check error messages point to library code (point of check) rather than user code (point of error) HOT 1
- Function variables can collide with C identifiers HOT 1
- Confusing errors with varargs
- Permit function returns in generic instantiation, not just types
- Broken preprocessor after 9faaa66e HOT 3
- Comparison of a pointer with nil generates unexpected C code HOT 2
- we need nelua debugger(vscode) or tell us how to debug my app? HOT 2
- Compatible with Lua C plug-in HOT 2
- LFS: can't change directory with `lfs.chdir` HOT 1
- error while compiling when a source file starts with a number HOT 1
- Compiler hangs when cgenerator.lua errors
- Can't compile fibonacci example
- Can't compile fibonacci example
- Support actual closures via struct
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nelua-lang.