开源软件名称(OpenSource Name):leegao/LuaInLua开源软件地址(OpenSource Url):https://github.com/leegao/LuaInLua开源编程语言(OpenSource Language):Lua 100.0%开源软件介绍(OpenSource Introduction):Welcome to Lua, in Lua!Hey! I'm glad you've found me. I am a somewhat correct compiler for the Lua 5.2 language into the Lua Bytecode format, but more than that, I hope that I can become a useful guide for those who are interested in language implementation. I grew out of a month-long sprint from an initiative to complete a side-project within the span of the November of 2015, but I wanted to keep tredding on. InstallationMake sure that you have Lua 5.2 with git clone https://github.com/leegao/LuaInLua.git
cd LuaInLua
luarocks make And voila, the package Usage
lua.lua
leegao@DESKTOP-3RST9I3:/mnt/c/Users/leegao/Documents/IdeaProjects/LuaInLua$ lua.lua testing/hello_world.lua -d
Level 0
Code
1 (line 10) CLOSURE(A=r(0), Bx=v(0))
2 (line 13) MOVE(A=r(1), B=r(0))
3 (line 13) LOADK(A=r(2), Bx=Kst(0))
4 (line 13) LOADK(A=r(3), Bx=Kst(1))
5 (line 13) LOADK(A=r(4), Bx=Kst(2))
6 (line 13) CALL(A=r(1), B=v(4), C=v(1))
7 (line 9) RETURN(A=r(0), B=v(1))
Constants
1 1
2 2
3 3
Upvalues
0 _ENV upval 0
Level 1
Code
1 (line 10) GETTABUP(A=r(0), B=v(0), C=Kst(0):rk(256))
2 (line 10) VARARG(A=r(1), B=v(0))
3 (line 10) CALL(A=r(0), B=v(0), C=v(1))
4 (line 10) RETURN(A=r(0), B=v(1))
Constants
1 print
Upvalues
0 _ENV upval 0
--- END OF DUMP ---
1 2 3 Additionally, if you don't specify a file to run, Compilation APIYou can directly invoke the local luac = require 'luainlua.luac'
local func, bytecode, proto, dumper = luac.compile('print("Hello World")')
-- Inspect the disassembly of bytecode
dumper(proto)
-- Run func
func() There's a wealth of internal APIs revolved around the compilation process ( Bootstrapping TestThe ultimate test of a compiler written in its own language is the bootstrap test. That is, can we use the compiler to compile itself, and then use the resulting compiler to compile an arbitrary program. Here is -- lua.lua
luac = require 'luainlua.luac'
func, bytecode, prototype, dumper = luac.compile('print("Hello World")')
dumper(prototype) -- Human readable disassembly
func()
-- <ctrl + d>
--[[ Level 0
Code
1 (line 1) GETTABUP(A=r(0), B=v(0), C=Kst(0):rk(256))
2 (line 1) LOADK(A=r(1), Bx=Kst(1))
3 (line 1) CALL(A=r(0), B=v(2), C=v(1))
4 (line 1) RETURN(A=r(0), B=v(1))
Constants
1 print
2 Hello World
Upvalues
0 _ENV upval 0
Hello World
]]-- ParserIn addition to the vanilla compiler, A sample grammar for the handmade parser generator is given here:
This presents a lambda-calculus-esque language. PhilosophyWhy Lua? If you're interested in language implementation, you typically see two types of tutorials out there depending on your background.
I've tried both of these approaches, and neither worked. Obviously, the Dragon Book is to be blamed. My philosophy: don't read, just do. This is one of the core tenets of people who love to program. No matter how hard you try, you will rarely be able to have a full end-to-end perspective on a large and complex system. Stop falling into never-ending rabbit holes and start iterating. There's no better way to learn about a domain than to get your hands dirty. So this is my sell for my grand ambition of getting free labor. Lua turns out to be a rather curious language, both from the language design perspective as well as the choice of tooling that it uses. No matter if you're a fresh developer or a seasoned Clang hacker (that's me!), you're sure to find something fresh and interesting because the design choices made by Lua are somewhat unconventional. So come hack along and squash some bugs. Hopefully you'll gain some valuable insights that'll finally bridge you across the deep chasm between the "How to even Compilers for Dummies" and actually fleshing out your own language. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论