/
freewaffle
/
descriptc
Обзор
Документация
Войти
/
freewaffle
/
descriptc
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
descript/compiler.lua
98 строк
3 KB
freewaffle
v56
13 дек 2025, 00:27
Не верифицирован
13 дек 2025, 00:27
651d253
Код
Авторство
О чём код?
local lang = { token = require "descript.lang.token", log = require "descript.lang.log" } local parsers = { token_collector = require "descript.parsers.tokcol", ast_generator = require "descript.parsers.ast" } local props = require "descript.lang.props" local compiler = {} --- Компилирует исходный код из `text` и (TODO) возвращает байт-код. --- Возвращает `nil` при ошибке. --- --- @param id string --- @param text string function compiler.compile(id, text) if type(id) ~= "string" then error("param `id`: expected string, got " .. type(id), 2) return nil end if type(text) ~= "string" then error("param `text`: expected string, got " .. type(id), 2) return nil end local ok, tokens, groups, ast ok, tokens = pcall(parsers.token_collector, id, text) do if props.debug then print("collect_tokens: " .. (tokens ~= nil and "OK" or "FAIL")) end if not tokens or not ok then return nil else if props.debug then pcall(lang.token.list_tokens, tokens) end --[[ на такое присвоение ругается линтер, так как "тип `nil` не совпадает с `string`". как оказалось, даже без присвоения, сборщик мусора всё равно освобождает память (из показаний разницы в используемой памяти), поэтому выключено (но в следующих блоках всё равно будет присвоение на `nil`, так как лучше явно указывать, что ресурс больше не используется. здесь исключение из правила) ]] --- @diagnostic disable-next-line: cast-local-type -- text = nil end end ok, groups = pcall(lang.token.get_groups, tokens) do if props.debug then print("get_groups: " .. (groups ~= nil and "OK" or "FAIL") .. "\n") end if not groups or not ok then return nil else if props.debug then pcall(lang.token.list_groups, groups) end --- @diagnostic disable-next-line: cast-local-type tokens = nil end end ok, ast = pcall(parsers.ast_generator, id, groups) do if props.debug then print("generate_ast: " .. (ast ~= nil and "OK" or "FAIL") .. "\n") end if not ast or not ok then return nil else if props.debug then -- TODO: pcall(dbg_ast, ast) end --- @diagnostic disable-next-line: cast-local-type groups = nil end end -- TODO: вернуть байткод end return compiler