GithubHelp home page GithubHelp logo

Comments (3)

edubart avatar edubart commented on September 14, 2024 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.

stefanos82 avatar stefanos82 commented on September 14, 2024 1

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

stefanos82 avatar stefanos82 commented on September 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.