Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What's wrong with my simple compiler?

Asked by
ZeroBits 142
9 years ago

EDIT; I solved it, the reason it wasn't working was that it was in StarterGui, and I didn't spawn a player, I'm an idiot. it's still pretty broken though, so if you want to help me iron out some of the problems, I'd appreciate it.

I've been working on a simple compiler, but it just won't do anything, what's wrong with it? the value of input is '5+7/2-1' none of the print functions are called, and the script isn't disabled.

The OS Modulescript it imports from;

function Write(s) -- Writes a string
    if s ~= nil then
        print(s)
    else
        print("")
    end
end

function WriteLn(s) -- Writes a string to a new line
    if s ~= nil then
        print(s)
    else
        print("")
    end
end

function Halt() -- Halts the program
    print("HALT")
end

function In(x,t) -- checks to see if an item is in a table
    for i = 1,#t do
        if t[i] == x then
            return true
        end
    end
    return false
end

Alphas = -- All Letters
    {'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
    'O','P','Q','R','S','T','U','V','W','X','Y','Z'}

Digits =  -- All Digits
    {'1','2','3','4','5','6','7','8','9','0'}

D = {nil,nil,nil,nil,nil,nil,nil,nil}

S = {}

return {Write,WriteLn,Halt,In,Alphas,Digits,'ready',D,S}

The Broken Script

print("start")

DEBUG = true -- set to false to disable debug printing

input = script.Parent.Parent.input -- fake input

print("1")
-- Necessary Functions
OS = require(script.Parent.OS)
print(OS[7])
D = OS[8]
SP = OS[9]
print('2')

Write = OS[1]
WriteLn = OS[2]
Halt = OS[3]
In = OS[4]

rd = 0
function Read()
    rd = rd + 1
    local re = string.sub(input.Value,rd,rd+1)
    PrintD(re)
    return re
end

function SP_push(x)
    PrintD(x)
    table.insert(SP, x)
end

function SP_pop()
    local rt = SP[-1]
    PrintD(rt)
    table.remove(SP)
    return rt
end
-- Debug Function
function PrintD(s)
    if DEBUG == true then
    if s ~= nil then
        WriteLn("DEBUG; "..s)
    else
        WriteLn("DEBUG MODE")
    end
    end
end 
-- Necessary Tables
Alphas = OS[5]
Digits = OS[6]
-- Compiler
local TAB = "   " -- Tab
local Look = "" -- lookup a char

local function GetChar() -- get Look
    Look = Read()
end

local function Error(s) -- Write an error
    warn("Error: "..s..".")
end 

local function Abort(s) -- Abort the program
    Error(s)
    Halt()
end

local function Expected(s) -- Write a "Char Expected" Error
    Abort(s.. " Expected")
end

local function Match(x) -- Match Characters
    if Look == x then GetChar() else
        Expected('"'..x..'"')
    end
end

local function IsAlpha(c) -- Check if a character is an Alpha Letter
    return In(string.upper(c),Alphas)
end


local function IsDigit(c) -- Check if a character is a Digit
    return In(c,Digits)
end

local function GetName() -- Get a Name
    if not IsAlpha(Look) then Expected('Name') end
    PrintD(string.upper(Look))
    return string.upper(Look)
end

local function GetNum() -- Get a Number
    if not IsDigit(Look) then Expected('Integer') end
    PrintD(Look)
    return Look
end

local function Emit(s) -- Print a String
    Write(TAB..s)
end

local function EmitLn(s) -- Print a String on a new line
    Emit(s)
    WriteLn()
end

--local function Term() -- Term
--  --Look = '7'
--  D[1] = GetNum()
--end

local function Factor()
    D[1] = GetNum()
end

local function Multiply()
    Match('*')
    Factor()
    EmitLn(SP_pop() * D[1])
end

local function Divide()
    Match('/')
    Factor()
    D[2] = SP_pop
    EmitLn(D[2]/D[1])
end

local function Term()
    Factor()
    while Look == '*' or Look == '?' do
        SP_push(D[1]) D[1] = nil

        if Look == '*' then
            Multiply()
        elseif Look == '/' then
            Divide()
        else Expected("Mulop") end
    end
end

local function Add() -- add
    Match('+')
    Term()
    EmitLn(SP_pop() + D[1])
end

local function Subtract() -- subtract
    Match('-')
    Term()
    EmitLn(SP_pop()-D[1])
end

local function Expression() -- Expressions
    Term()
    while Look == '+' or Look == '-' do
        SP_push(D[1]) D[1] = nil

        if Look == '+' then
            Add()
        elseif Look == '-' then
            Subtract()
        else Expected("Addop") end

    end
end

local function Init() -- Initiate
    GetChar()
end

local function begin() -- Start Function
    WriteLn("ZerOS")
    PrintD()
    Init()
    Expression()
end

begin() -- Run

0
On line 129, there's was a '?' instead of a forward slash. that's fixed, still not working, so it wasn't the problem. ZeroBits 142 — 9y

Answer this question