惠民建设局网站是哪个,软件开发培训机构找极客时间,编程scratch网站,连云港做网站企业本文目录 1、引言2、环境准备2.1 源码下载2.2 项目构建环境准备 3、项目编译3.1 添加main.c3.2 Kconfig选择模块3.3 项目构建3.4 项目编译 4、运行 文章对应视频教程#xff1a; 在下方喔 ~~~ 欢迎关注 点击图片或链接访问我的B站主页~~~ lau解释器移植与功能验证 1、引言
本… 本文目录 1、引言2、环境准备2.1 源码下载2.2 项目构建环境准备 3、项目编译3.1 添加main.c3.2 Kconfig选择模块3.3 项目构建3.4 项目编译 4、运行 文章对应视频教程 在下方喔 ~~~ 欢迎关注 点击图片或链接访问我的B站主页~~~ lau解释器移植与功能验证 1、引言
本文将介绍如何将Lua解释器移植到标准的ANSI C环境。 实现ANSI C的移植就可以实现在嵌入式各类板卡的移植。 当然你的板卡得具备ANSI C环境一般情况keil、GCC都会有C库实现。 关于Lua解释器的重要性或者有何特殊指出可以看看我得上篇文章 《可用于嵌入式的解释器调研对比及lua解释器介绍》 2、环境准备
2.1 源码下载
您可以在Lua官方网站上下载Lua解释器的源代码。以下是Lua官方网站的链接
Lua解释器下载
在该网页上点击的Lua解释器源代码的下载链接图中红色框部分进行源代码下载可能有点慢。 下载完成后进行接下得到下图的文件提取其中的src文件夹将src中的makefile、lua.c、luac.c文件删除待用。
2.2 项目构建环境准备
参考往期博客《CmakeKconfig项目构建》搭建一个空的项目框架用来测试Lua是否移植成功。
复制项目构建模板修改名字修改CMakeLists.txt和kconfig文件方便后续模块化移植到自己的项目中目录结构如下。 这里也可以直接复制到keil工程中只要你的环境具有ANSI C接口即可。
将前面准备好的源码复制到source/lua文件夹下注意是将src中的makefile、lua.c、luac.c文件删除后剩下的代码。 3、项目编译
3.1 添加main.c
我们需要在main.c中实现对lua代码的解释功能。 由于我在window环境下我可以很轻松的打开文件所以直接采用的文件读取的方式执行。 大家也可以直接将代码写在内存中类似与下面这种方式
char buff[] printf(123);我这边的代码比较复杂还是用读取文件实现。
main.c内容如下
#include stdio.h
#include stdlib.h#include lua.h
#include lauxlib.h
#include lualib.h
#include string.h#define BUFFER_SIZE 1024*1024*1static lua_State *L NULL ;int lua_deal_line(const char *str)
{int ret luaL_dostring(L,str);if(ret ! LUA_OK){printf(%s\r\n,lua_tostring(L ,-1));return -1;} return 0;
}int main(void)
{L luaL_newstate();luaL_openlibs(L);FILE *file;char buffer[BUFFER_SIZE] {0};size_t read_size;// 打开文件file fopen(test.lua, rb); // 使用rb模式以二进制方式读取文件if (file NULL) {perror(无法打开文件);return 1;}// 读取文件内容到缓冲区read_size fread(buffer, 1, BUFFER_SIZE - 1, file); // 保留一个字节给\0if (read_size 0 ferror(file)) {perror(read file error\r\n);fclose(file);return 1;}// 添加字符串末尾的空字符buffer[read_size] \0;// 关闭文件fclose(file);lua_deal_line(buffer);lua_close(L);return 1;
}创建test.lua文件放在当前路径下。
-- Lua 基本语法
print(Running basic syntax test...)
assert(true) -- 如果代码运行到这里没有错误发生那么基本语法测试通过-- Lua 数据类型
print(Running data type test...)
local num 10 -- number
local str Hello -- string
local bool true -- boolean
local tbl {1, 2, 3} -- table
local func function() return I am a function end -- function
assert(type(num) number)
assert(type(str) string)
assert(type(bool) boolean)
assert(type(tbl) table)
assert(type(func) function)-- Lua 变量
print(Running variable test...)
local localVar I am local
_G.globalVar I am global
assert(localVar I am local)
assert(_G.globalVar I am global)-- Lua 循环
print(Running loop test...)
local sum 0
for i 1, 5 dosum sum i
end
assert(sum 15)sum 0
local i 1
while i 5 dosum sum ii i 1
end
assert(sum 15)-- Lua 流程控制
print(Running control flow test...)
local x 10
local result
if x 5 thenresult greater
elseif x 5 thenresult equal
elseresult less
end
assert(result greater)-- Lua 函数
print(Running function test...)
function add(a, b)return a b
end
assert(add(5, 3) 8)-- Lua 运算符
print(Running operator test...)
local a, b 10, 20
assert(a b 30)
assert(a - b -10)
assert(a * b 200)
assert(a / b 0.5)
assert(a % b 10)
assert(a ^ 2 100)-- Lua 字符串
print(Running string test...)
local s Lua
assert(#s 3)
assert(s .. programming Lua programming)-- Lua 数组
print(Running array test...)
local arr {10, 20, 30, 40, 50}
assert(arr[1] 10)
assert(#arr 5)-- Lua 迭代器
print(Running iterator test...)
local function squareIterator(max, current)current current 1if current max thenreturn current, current * currentend
end
local results {}
for i, n in squareIterator, 5, 0 dotable.insert(results, n)
end
assert(#results 5)
assert(results[1] 1)
assert(results[5] 25)-- Lua table(表)
print(Running table test...)
local person {name John, age 30}
assert(person.name John)
assert(person.age 30)-- Lua 模块与包
print(Running module test...)
-- 假设有一个 module_name.lua 文件内容如下
-- local M {}
-- function M.hello() return Hello, module! end
-- return M
-- local module require(module_name)
-- assert(module.hello() Hello, module!)-- Lua 元表(Metatable)
print(Running metatable test...)
local mt {__add function(table1, table2)local sum {}for k, v in pairs(table1) dosum[k] v table2[k]endreturn sumend
}
local t1 {1, 2, 3}
local t2 {4, 5, 6}
setmetatable(t1, mt)
local t3 t1 t2
assert(t3[1] 5)
assert(t3[2] 7)
assert(t3[3] 9)-- Lua 协同程序(coroutine)
-- print(Running coroutine test...)
-- local co coroutine.create(function()
-- for i 1, 5 do
-- coroutine.yield(i)
-- end
-- end)
-- local _, first coroutine.resume(co)
-- assert(first 1)
-- local _, second coroutine.resume(co)
-- assert(second 2)-- Lua 文件 I/O
-- print(Running file I/O test...)
-- local file io.open(test.txt, w)
-- file:write(Hello, file!)
-- file:close()
-- file io.open(test.txt, r)
-- local content file:read(*all)
-- file:close()
-- assert(content Hello, file!)-- Lua 错误处理
print(Running error handling test...)
local status, err pcall(function()error(An error occurred)
end)
assert(not status)
assert(string.find(err, An error occurred))-- Lua 调试(Debug)
-- print(Running debug test...)
-- local debug require(debug)
-- local traceback
-- local function myfunc()
-- traceback debug.traceback(Stack trace)
-- end
-- myfunc()
-- assert(string.find(traceback, Stack trace))-- Lua 垃圾回收
print(Running garbage collection test...)
local tbl {1, 2, 3}
setmetatable(tbl, {__gc function() print(Garbage collected) end})
tbl nil
collectgarbage() -- 在控制台中应能看到 Garbage collected-- Lua 面向对象
print(Running object-oriented test...)
local Animal {name , age 0}
function Animal:new(o, name, age)o o or {}setmetatable(o, self)self.__index selfself.name name or self.age age or 0return o
end
function Animal:speak()return I am .. self.name .. , .. self.age .. years old.
endlocal dog Animal:new(nil, Dog, 5)
assert(dog:speak() I am Dog, 5 years old.)print(All tests passed!) 3.2 Kconfig选择模块
在project\pro1目录下打开powershell输入python .\ck_script.py cn进入到图形配置界面。 确保回车选中该模块再按Q Y按键退出。 3.3 项目构建
在powershell输入python .\ck_script.py b进行项目构建构建完成后对文件的编译规则就已经生成。 3.4 项目编译
在powershell输入python .\ck_script.py m进行项目编译结果如下。 4、运行
在powershell输入.\lua.exe运行程序结果如下。 这个Lua脚本验证了我们需要的基本语法、数据类型、变量、循环、流程控制、函数、运算符、字符串、数组、迭代器、table(表)、元表(Metatable)、错误处理、垃圾回收、面向对象等功能。 时间流逝、年龄增长是自己的磨炼、对知识技术的应用还有那不变的一颗对嵌入式热爱的心 到这里就结束了希望大家给我的文章和B站视频 点赞o(▽)、关注(o)/~、评论(▽)