com.sr.eb.compiler.Main.java Source code

Java tutorial

Introduction

Here is the source code for com.sr.eb.compiler.Main.java

Source

package com.sr.eb.compiler;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import com.sr.eb.compiler.lexer.Lexer;
import com.sr.eb.compiler.lexer.Token;
import com.sr.eb.compiler.parser.Parser;
import com.sr.eb.compiler.parser.SyntaxTree;
import com.sr.eb.compiler.util.ASTPrettyPrinter;
import com.sr.eb.compiler.util.Utils;

/*
 * E Copyright (C) 2014 Matt Peterson, Evan Welsh, and Jackson Isted. Contact
 * us at <a href="eflatlang@gmail.com">EFlat-Lang</a>
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>
 */

public class Main {
    public static void main(String[] args) throws Throwable {
        try {
            Options options = new Options();

            options.addOption("v", "verbose", false, "Enables verbose compiler output.");
            options.addOption("ee", "easterEgg", false, "Enables compiler easter eggs, DO NOT USE!");

            CommandLineParser parser = new BasicParser();
            CommandLine cmd = parser.parse(options, args);

            if (cmd.getArgs().length > 0) {
                int start = -1;
                while (true) {
                    start++;
                    if (options.getOption(cmd.getArgs()[start]) == null)
                        break;
                }

                String[] sourceFiles = Arrays.copyOfRange(cmd.getArgs(), start, cmd.getArgs().length);

                // TODO
            } else {
                System.out.println("Expected source files, got none!");
                System.out.println("Usage: ebc [options] <source files>");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        /* ----- Testing ----- */
        Lexer lexer = Lexer.newInstance();
        Parser parser = Parser.newInstance();

        try (InputStream in = new FileInputStream("test.eb")) {
            lexer.reset(Utils.readAll(in));
        }

        List<Token> tokens = lexer.tokenize();
        parser.reset(tokens);
        SyntaxTree t = parser.parse();

        ASTPrettyPrinter.print(t);
    }
}