commit fc369242fc4d8d26841940659c9e6ef95d995ad1 from: Onana Onana Xavier Manuel date: Mon Sep 28 05:41:08 2026 UTC The 49 commit Added string parsing. Added u8 keyword for string. commit - 923305236bcc5057428793a83fad7fc77f2ca4f3 commit + fc369242fc4d8d26841940659c9e6ef95d995ad1 blob - a422333b0d09daf877bbbd0e4082f9954ede2c9f blob + d007f719884f24c68f2545876792f134cddededd --- tools/6c +++ tools/6c @@ -10,14 +10,20 @@ local mnemonics = { "mov", "cli", "hlt", "ret", "sti", local arithmetics = { "+", "-", "*", "/", "^", "~", "|", "&" } local symbols = { ",", "[", "]", "(", ")", "{", "}", "*", "/", "-", - "+", "!", "~", "#", "?", ";", ":" } + "+", "!", "~", "#", "?", ";", ":", "=" } local regs8 = { "al", "ah", "bl", "bh", "cl", "ch", "dl", "dh" } +local regs16 = { "ax", "bx", "cx", "dx" } + +local ptr16 = { "si", "di", "bp", "sp" } + local asmkwd = { "asm" } -local tk_misc = { "hexadecimal", "integer", "id", "\n" } +local ckwd = { "u8" } +local tk_misc = { "hexadecimal", "integer", "id", "string", "\n" } + local tk_type = { } local count = 0 @@ -36,6 +42,16 @@ for i = 1, #regs8 do tk_type[regs8[i]] = count end +for i = 1, #regs16 do + count = count + 1 + tk_type[regs16[i]] = count +end + +for i = 1, #ptr16 do + count = count + 1 + tk_type[ptr16[i]] = count +end + for i = 1, #asmkwd do count = count + 1 tk_type[asmkwd[i]] = count @@ -45,6 +61,12 @@ for i = 1, #tk_misc do count = count + 1 tk_type[tk_misc[i]] = count end + +for i = 1, #ckwd do + count = count + 1 + tk_type[ckwd[i]] = count +end + local function cincadd() count = count + 1 return count @@ -549,6 +571,25 @@ local function readint(line, s) return table.concat(buf) end +local function readstr(line, s) + local sub = { } + + while true do + s = s + 1 + if s > #line then break end + + local char = line:sub(s, s) + + if char == "\"" or char == nil then + break + end + + table.insert(sub, char) + end + + return table.concat(sub) +end + local function readid(line, s) local len = #line local buf = { } @@ -610,6 +651,11 @@ while true do emittoken(nline, ncol, tk_type["integer"], #data, data) end i = i + #data + elseif char == "\"" then + local data = readstr(line, i) + i = i + #data + 2 + emittoken(nline, ncol, tk_type["string"], #data, data) + elseif char == " " or char == "\t" then i = i + 1 else blob - ab515874c65a09776f2571749e6e6ee0f8379fe0 blob + 75d71488a3c955c3dd92adac692d57c728bd373b --- tools/6l +++ tools/6l @@ -10,14 +10,20 @@ local mnemonics = { "mov", "cli", "hlt", "ret", "sti", local arithmetics = { "+", "-", "*", "/", "^", "~", "|", "&" } local symbols = { ",", "[", "]", "(", ")", "{", "}", "*", "/", "-", - "+", "!", "~", "#", "?", ";", ":" } + "+", "!", "~", "#", "?", ";", ":", "=" } local regs8 = { "al", "ah", "bl", "bh", "cl", "ch", "dl", "dh" } +local regs16 = { "ax", "bx", "cx", "dx" } + +local ptr16 = { "si", "di", "bp", "sp" } + local asmkwd = { "asm" } -local tk_misc = { "hexadecimal", "integer", "id", "\n" } +local ckwd = { "u8" } +local tk_misc = { "hexadecimal", "integer", "id", "string", "\n" } + local tk_type = { } local count = 0 @@ -36,6 +42,16 @@ for i = 1, #regs8 do tk_type[regs8[i]] = count end +for i = 1, #regs16 do + count = count + 1 + tk_type[regs16[i]] = count +end + +for i = 1, #ptr16 do + count = count + 1 + tk_type[ptr16[i]] = count +end + for i = 1, #asmkwd do count = count + 1 tk_type[asmkwd[i]] = count @@ -45,6 +61,12 @@ for i = 1, #tk_misc do count = count + 1 tk_type[tk_misc[i]] = count end + +for i = 1, #ckwd do + count = count + 1 + tk_type[ckwd[i]] = count +end + local function cincadd() count = count + 1 return count @@ -496,6 +518,7 @@ end if output == nil then output = string.gsub(input, "%.%w+$", "") + output = "6." .. output end local src = io.open(input, "rb") @@ -503,7 +526,7 @@ if src == nil then perror("failed to open `" .. input .. "`") end -local dst = io.open("6." .. output, "wb") +local dst = io.open(output, "wb") if dst == nil then perror("failed to open `" .. output .. "`") end @@ -1045,17 +1068,72 @@ while true do end else if hdr.type == tk_type["asm"] then - local next = readhdr() - if next.type ~= tk_type["{"] then + local nxt = readhdr() + if nxt.type ~= tk_type["{"] then perror("missing { after asm, line " .. hdr.line .. " column " .. hdr.col) end parser.inasm = true + + elseif hdr.type == tk_type["/"] then + local nxt = readhdr() + + -- COMMENTS + if nxt.type == tk_type["/"] then + while nxt.type ~= tk_type["\n"] do + nxt = readhdr() + end + elseif nxt.type == tk_type["*"] then + while true do + nxt = readhdr() + + if nxt == nil then + break + end + + if nxt.type == tk_type["*"] then + nxt = readhdr() + if nxt == nil or nxt.type == tk_type["/"] then + break + end + end + end + end + + elseif hdr.type == tk_type["u8"] then + local n = readhdr() + + if n.type == tk_type["*"] then + local name = readhdr() + + if name.type ~= tk_type["id"] then + perror("missing name for symbol at line " .. hdr.line) + end + + local equal = readhdr() + + if equal.type ~= tk_type["="] then + perror("missing equal sign next to " .. name.raw .. " at line " .. hdr.line) + end + + local data = readhdr() + + if data.type ~= tk_type["string"] then + perror("missing string next to " .. name.raw .. " at line " .. hdr.line) + end + + local semi = readhdr() + + if semi.type ~= tk_type[";"] then + perror("missing closure on line " .. name.line) + end + + offset = offset + #data.raw + 1 + end + end end end -print(offset) - ----------------------------------------------------------------------- -- 2nd pass binary generation ----------------------------------------------------------------------- @@ -1177,6 +1255,55 @@ while true do if hdr.type == tk_type["asm"] then parser.inasm = true readhdr() -- Skip `{` + + elseif hdr.type == tk_type["u8"] then + local n = readhdr() + + if n.type == tk_type["*"] then + local name = readhdr() + if name.type == tk_type["id"] then + local equal = readhdr() + + if equal.type == tk_type["="] then + local data = readhdr().raw + + for i = 1, #data do + local b = string.byte(data:sub(i, i)) + writeu8(dst, b) + end + + writeu8(dst, 0) + coffset = coffset + #data + 1 + readhdr() + end + end + end + + elseif hdr.type == tk_type["/"] then + local nxt = readhdr() + + -- COMMENTS + if nxt.type == tk_type["/"] then + while nxt.type ~= tk_type["\n"] do + nxt = readhdr() + end + elseif nxt.type == tk_type["*"] then + while true do + nxt = readhdr() + + if nxt == nil then + break + end + + if nxt.type == tk_type["*"] then + nxt = readhdr() + if nxt == nil or nxt.type == tk_type["/"] then + break + end + end + end + end + end end end blob - bebf513b56d50f1ce90cb3994db35cbbfa782e46 blob + 39c2bc0e58de7275b4ad92debad6d562d422a564 --- tools/boot.sc +++ tools/boot.sc @@ -1,5 +1,25 @@ +/* + * boot.sc + * + * Just a simple test to see if comments work in the source file + * as well as the assembly blocks. The assembly blocks can + * call sections between each other without being in the same + * block. + * + * The next step is to also support things like db, dd, dw and dq. + * This will allow things like moving string into registers. + */ + +/* SKIP BLOCK */ asm { -_start: +entry: + jmp start +} + +u8 *msg = "Hello World"; + +asm { +start: mov bl, 0hff jmp inter print: @@ -15,6 +35,7 @@ inter: asm { mov cl, 0 + call print second: mov ah, 0h0e mov al, 0h41 blob - 4f1f15f1abbc7a87e724565092403922576b4833 blob + 862da8cc04649469c596765516f807dd1ec01c3b --- tools/compiler.lua +++ tools/compiler.lua @@ -72,6 +72,25 @@ local function readint(line, s) return table.concat(buf) end +local function readstr(line, s) + local sub = { } + + while true do + s = s + 1 + if s > #line then break end + + local char = line:sub(s, s) + + if char == "\"" or char == nil then + break + end + + table.insert(sub, char) + end + + return table.concat(sub) +end + local function readid(line, s) local len = #line local buf = { } @@ -133,6 +152,11 @@ while true do emittoken(nline, ncol, tk_type["integer"], #data, data) end i = i + #data + elseif char == "\"" then + local data = readstr(line, i) + i = i + #data + 2 + emittoken(nline, ncol, tk_type["string"], #data, data) + elseif char == " " or char == "\t" then i = i + 1 else blob - fde3083bfbc7d30f4de8c831e63a279544968e3c blob + 08acb106c046ffe78b27b2a9314e57629653da97 --- tools/linker.lua +++ tools/linker.lua @@ -19,6 +19,7 @@ end if output == nil then output = string.gsub(input, "%.%w+$", "") + output = "6." .. output end local src = io.open(input, "rb") @@ -26,7 +27,7 @@ if src == nil then perror("failed to open `" .. input .. "`") end -local dst = io.open("6." .. output, "wb") +local dst = io.open(output, "wb") if dst == nil then perror("failed to open `" .. output .. "`") end @@ -568,17 +569,72 @@ while true do end else if hdr.type == tk_type["asm"] then - local next = readhdr() - if next.type ~= tk_type["{"] then + local nxt = readhdr() + if nxt.type ~= tk_type["{"] then perror("missing { after asm, line " .. hdr.line .. " column " .. hdr.col) end parser.inasm = true + + elseif hdr.type == tk_type["/"] then + local nxt = readhdr() + + -- COMMENTS + if nxt.type == tk_type["/"] then + while nxt.type ~= tk_type["\n"] do + nxt = readhdr() + end + elseif nxt.type == tk_type["*"] then + while true do + nxt = readhdr() + + if nxt == nil then + break + end + + if nxt.type == tk_type["*"] then + nxt = readhdr() + if nxt == nil or nxt.type == tk_type["/"] then + break + end + end + end + end + + elseif hdr.type == tk_type["u8"] then + local n = readhdr() + + if n.type == tk_type["*"] then + local name = readhdr() + + if name.type ~= tk_type["id"] then + perror("missing name for symbol at line " .. hdr.line) + end + + local equal = readhdr() + + if equal.type ~= tk_type["="] then + perror("missing equal sign next to " .. name.raw .. " at line " .. hdr.line) + end + + local data = readhdr() + + if data.type ~= tk_type["string"] then + perror("missing string next to " .. name.raw .. " at line " .. hdr.line) + end + + local semi = readhdr() + + if semi.type ~= tk_type[";"] then + perror("missing closure on line " .. name.line) + end + + offset = offset + #data.raw + 1 + end + end end end -print(offset) - ----------------------------------------------------------------------- -- 2nd pass binary generation ----------------------------------------------------------------------- @@ -700,6 +756,55 @@ while true do if hdr.type == tk_type["asm"] then parser.inasm = true readhdr() -- Skip `{` + + elseif hdr.type == tk_type["u8"] then + local n = readhdr() + + if n.type == tk_type["*"] then + local name = readhdr() + if name.type == tk_type["id"] then + local equal = readhdr() + + if equal.type == tk_type["="] then + local data = readhdr().raw + + for i = 1, #data do + local b = string.byte(data:sub(i, i)) + writeu8(dst, b) + end + + writeu8(dst, 0) + coffset = coffset + #data + 1 + readhdr() + end + end + end + + elseif hdr.type == tk_type["/"] then + local nxt = readhdr() + + -- COMMENTS + if nxt.type == tk_type["/"] then + while nxt.type ~= tk_type["\n"] do + nxt = readhdr() + end + elseif nxt.type == tk_type["*"] then + while true do + nxt = readhdr() + + if nxt == nil then + break + end + + if nxt.type == tk_type["*"] then + nxt = readhdr() + if nxt == nil or nxt.type == tk_type["/"] then + break + end + end + end + end + end end end blob - f99efa741ad5ace5d4e48cf07471a770ffdfe6cf blob + 4f2c6e65529c5e9cde697a510410e1f6ba2ab2f8 --- tools/util.lua +++ tools/util.lua @@ -8,14 +8,20 @@ local mnemonics = { "mov", "cli", "hlt", "ret", "sti", local arithmetics = { "+", "-", "*", "/", "^", "~", "|", "&" } local symbols = { ",", "[", "]", "(", ")", "{", "}", "*", "/", "-", - "+", "!", "~", "#", "?", ";", ":" } + "+", "!", "~", "#", "?", ";", ":", "=" } local regs8 = { "al", "ah", "bl", "bh", "cl", "ch", "dl", "dh" } +local regs16 = { "ax", "bx", "cx", "dx" } + +local ptr16 = { "si", "di", "bp", "sp" } + local asmkwd = { "asm" } -local tk_misc = { "hexadecimal", "integer", "id", "\n" } +local ckwd = { "u8" } +local tk_misc = { "hexadecimal", "integer", "id", "string", "\n" } + local tk_type = { } local count = 0 @@ -34,6 +40,16 @@ for i = 1, #regs8 do tk_type[regs8[i]] = count end +for i = 1, #regs16 do + count = count + 1 + tk_type[regs16[i]] = count +end + +for i = 1, #ptr16 do + count = count + 1 + tk_type[ptr16[i]] = count +end + for i = 1, #asmkwd do count = count + 1 tk_type[asmkwd[i]] = count @@ -43,6 +59,12 @@ for i = 1, #tk_misc do count = count + 1 tk_type[tk_misc[i]] = count end + +for i = 1, #ckwd do + count = count + 1 + tk_type[ckwd[i]] = count +end + local function cincadd() count = count + 1 return count