Example #1
0
    static void Main(string[] args)
    {
        using (var v = new Squirrel())
        {
            v.SetPrintFunc(PrintFunc, ErrorFunc);
            v.PushRootTable();
            v.RegisterBlobLib();
            v.RegisterIOLib();
            v.RegisterSystemLib();
            v.RegisterMathLib();
            v.RegisterStringLib();

            // aux library
            // sets error handlers
            v.SetErrorHandlers();

            bool   justCompile = false;
            string outFile     = string.Empty;

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].StartsWith("-") && args[i].Length <= 1)
                {
                    switch (args[i][1])
                    {
                    case 'd': // DEBUG (debug infos)
                        v.EnableDebugInfo(true);
                        break;

                    case 'c':
                        justCompile = true;
                        break;

                    case 'o':
                        if (i < args.Length)
                        {
                            outFile = args[++i];
                        }
                        break;

                    case 'v':
                        PrintVersionInfo();
                        return;

                    case 'h':
                        PrintVersionInfo();
                        PrintUsage();
                        return;

                    default:
                        PrintVersionInfo();
                        Console.WriteLine("unknown parameter '-{0}'", args[i][1]);
                        PrintUsage();
                        return;
                    }
                }
                else
                {
                    string inFile = args[i++];

                    if (justCompile)
                    {
                        if (v.LoadFile(inFile, true) >= 0)
                        {
                            if (v.WriteClosureToFile(string.IsNullOrEmpty(outFile) ? "out.cnut" : outFile) >= 0)
                            {
                                return;
                            }
                        }
                    }
                    else
                    {
                        if (v.LoadFile(inFile, true) >= 0)
                        {
                            int callArgs = 1;
                            v.PushRootTable();

                            for (int j = i; j < args.Length; j++, callArgs++)
                            {
                                v.PushString(args[j], -1);
                            }

                            if (v.Call(callArgs, true, true) >= 0)
                            {
                                ObjectType type = v.GetType(-1);
                                if (type == ObjectType.Integer)
                                {
                                    int result = (int)type;
                                    v.GetInteger(-1, out result);
                                    Environment.Exit(result);
                                }
                            }
                        }
                    }

                    return;
                }
            }

            Interactive(v);
        }
    }