This commit is contained in:
arookas 2015-12-28 03:13:30 -05:00
parent aa61439095
commit f5394f56d0

View file

@ -5,14 +5,11 @@ using System.Text;
namespace arookas namespace arookas
{ {
static class SSC static class SSC {
{ static void Main(string[] args) {
static void Main(string[] args)
{
Message("ssc v0.1 arookas\n"); Message("ssc v0.1 arookas\n");
var cmd = new CommandLine(args); var cmd = new CommandLine(args);
if (cmd.Count == 0) if (cmd.Count == 0) {
{
Message("Usage: ssc -input <input.sun> [-output <output.sb>]\n"); Message("Usage: ssc -input <input.sun> [-output <output.sb>]\n");
Pause(); Pause();
Exit(1); Exit(1);
@ -21,11 +18,9 @@ namespace arookas
int exitCode = 0; int exitCode = 0;
string inputFile, outputFile; string inputFile, outputFile;
ReadCmdLine(cmd, out inputFile, out outputFile); ReadCmdLine(cmd, out inputFile, out outputFile);
using (var output = OpenOutput(outputFile)) using (var output = OpenOutput(outputFile)) {
{
var results = compiler.Compile(inputFile, output); var results = compiler.Compile(inputFile, output);
if (results.Success) if (results.Success) {
{
Message("Finished compiling in {0:F2}ms.\n", results.CompileTime.TotalMilliseconds); Message("Finished compiling in {0:F2}ms.\n", results.CompileTime.TotalMilliseconds);
Message(" Data count: {0}\n", results.DataCount); Message(" Data count: {0}\n", results.DataCount);
Message("Symbol count: {0}\n", results.SymbolCount); Message("Symbol count: {0}\n", results.SymbolCount);
@ -33,16 +28,13 @@ namespace arookas
Message(" - functions: {0}\n", results.FunctionCount); Message(" - functions: {0}\n", results.FunctionCount);
Message(" - variables: {0}\n", results.VariableCount); Message(" - variables: {0}\n", results.VariableCount);
} }
else else {
{ if (results.Error is sunSourceException) {
if (results.Error is sunSourceException)
{
var error = results.Error as sunSourceException; var error = results.Error as sunSourceException;
Error(" in file \"{0}\"\n at line {1}, col {2}\n\n{3}{4}", error.Location.File, error.Location.Line, error.Location.Column, GetErrorPreview(error.Location), error.Message); Error(" in file \"{0}\"\n at line {1}, col {2}\n\n{3}{4}", error.Location.File, error.Location.Line, error.Location.Column, GetErrorPreview(error.Location), error.Message);
exitCode = 1; exitCode = 1;
} }
else else {
{
var error = results.Error; var error = results.Error;
Error("{0}", error.Message); Error("{0}", error.Message);
exitCode = 1; exitCode = 1;
@ -53,14 +45,11 @@ namespace arookas
Exit(exitCode); Exit(exitCode);
} }
static Stream OpenOutput(string path) static Stream OpenOutput(string path) {
{ try {
try
{
return File.Create(path); return File.Create(path);
} }
catch catch {
{
Error("Failed to create output file '{0}'.", path); Error("Failed to create output file '{0}'.", path);
Pause(); Pause();
Exit(1); Exit(1);
@ -68,55 +57,45 @@ namespace arookas
return null; return null;
} }
static void ReadCmdLine(CommandLine cmd, out string inputFile, out string outputFile) // command-line
{ static void ReadCmdLine(CommandLine cmd, out string inputFile, out string outputFile) {
inputFile = null; inputFile = null;
outputFile = null; outputFile = null;
foreach (var prm in cmd) foreach (var prm in cmd) {
{ switch (prm.Name) {
switch (prm.Name)
{
case "-input": GetInput(prm, ref inputFile); break; case "-input": GetInput(prm, ref inputFile); break;
case "-output": GetOutput(prm, ref outputFile); break; case "-output": GetOutput(prm, ref outputFile); break;
} }
} }
if (inputFile == null) if (inputFile == null) {
{
Error("Missing -input option.\n"); Error("Missing -input option.\n");
Pause(); Pause();
Exit(1); Exit(1);
} }
if (outputFile == null) if (outputFile == null) {
{
outputFile = Path.ChangeExtension(inputFile, ".sb"); outputFile = Path.ChangeExtension(inputFile, ".sb");
} }
} }
static void GetInput(CommandLineParameter prm, ref string inputFile) static void GetInput(CommandLineParameter prm, ref string inputFile) {
{ if (inputFile != null) {
if (inputFile != null)
{
Error("Only one -input option is allowed.\n"); Error("Only one -input option is allowed.\n");
Pause(); Pause();
Exit(1); Exit(1);
} }
if (prm.Count != 1) if (prm.Count != 1) {
{
Error("Incorrect number of arguments in -input option.\n"); Error("Incorrect number of arguments in -input option.\n");
Pause(); Pause();
Exit(1); Exit(1);
} }
inputFile = prm[0]; inputFile = prm[0];
} }
static void GetOutput(CommandLineParameter prm, ref string outputFile) static void GetOutput(CommandLineParameter prm, ref string outputFile) {
{ if (outputFile != null) {
if (outputFile != null)
{
Error("Only one -output option is allowed.\n"); Error("Only one -output option is allowed.\n");
Pause(); Pause();
Exit(1); Exit(1);
} }
if (prm.Count != 1) if (prm.Count != 1) {
{
Error("Incorrect number of arguments in -output option.\n"); Error("Incorrect number of arguments in -output option.\n");
Pause(); Pause();
Exit(1); Exit(1);
@ -124,42 +103,34 @@ namespace arookas
outputFile = prm[0]; outputFile = prm[0];
} }
static string GetErrorPreview(sunSourceLocation location) // error preview
{ static string GetErrorPreview(sunSourceLocation location) {
Stream file; Stream file;
try try {
{
file = File.OpenRead(location.File); file = File.OpenRead(location.File);
} }
catch catch {
{
// simply don't do a preview if opening a file fails // simply don't do a preview if opening a file fails
return ""; return "";
} }
using (var reader = new StreamReader(file)) using (var reader = new StreamReader(file)) {
{
// skip to line // skip to line
for (var line = 1; line < location.Line; ++line) for (var line = 1; line < location.Line; ++line) {
{
reader.ReadLine(); reader.ReadLine();
} }
// generate column string // generate column string
var sb = new StringBuilder(); var sb = new StringBuilder();
var preview = reader.ReadLine(); var preview = reader.ReadLine();
sb.AppendLine(preview); sb.AppendLine(preview);
for (var column = 1; column < location.Column; ++column) for (var column = 1; column < location.Column; ++column) {
{
var c = preview[column - 1]; var c = preview[column - 1];
if (IsFullWidth(c)) if (IsFullWidth(c)) {
{
sb.Append(" "); // full-width hack sb.Append(" "); // full-width hack
} }
else if (c == '\t') else if (c == '\t') {
{
sb.Append('\t'); sb.Append('\t');
} }
else else {
{
sb.Append(" "); sb.Append(" ");
} }
} }
@ -168,30 +139,24 @@ namespace arookas
return sb.ToString(); return sb.ToString();
} }
} }
static bool IsFullWidth(char c) static bool IsFullWidth(char c) { return (c >= 0x2E80 && c <= 0x9FFF) || (c >= 0xFF00 && c <= 0xFFEF); }
{
return (c >= 0x2E80 && c <= 0x9FFF) || (c >= 0xFF00 && c <= 0xFFEF);
}
// output
static void Message(string format, params object[] args) { Console.Write(format, args); } static void Message(string format, params object[] args) { Console.Write(format, args); }
static void Warning(string format, params object[] args) static void Warning(string format, params object[] args) {
{
Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("WARNING:\n"); Console.Write("WARNING:\n");
Message(format, args); Message(format, args);
Console.ResetColor(); Console.ResetColor();
} }
static void Error(string format, params object[] args) static void Error(string format, params object[] args) {
{
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
Console.Write("ERROR:\n"); Console.Write("ERROR:\n");
Message(format, args); Message(format, args);
Console.ResetColor(); Console.ResetColor();
} }
[Conditional("DEBUG")] static void Pause()
{ [Conditional("DEBUG")] static void Pause() { Console.ReadKey(); }
Console.ReadKey();
}
static void Exit(int code) { Environment.Exit(code); } static void Exit(int code) { Environment.Exit(code); }
} }
} }