clean up usage for repl
This commit is contained in:
parent
107a01a39c
commit
1b51632360
1 changed files with 74 additions and 6 deletions
80
src/main.zig
80
src/main.zig
|
@ -17,7 +17,29 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const mem = @import("memory.zig");
|
const mem = @import("memory.zig");
|
||||||
const cpu_ = @import("cpu.zig");
|
const cpu_ = @import("cpu.zig");
|
||||||
|
|
||||||
var long_prompt = false;
|
var long_prompt = false;
|
||||||
|
|
||||||
|
const TOO_MANY_ARGUMENTS = "Wrong number of arguments";
|
||||||
|
const WRONG_ARGUMENT = "Invalid argument";
|
||||||
|
// COMMAND
|
||||||
|
const PROMPT_COMMAND = "prompt";
|
||||||
|
const SEGMENT_REGISTER_COMMAND = "sr";
|
||||||
|
const REGISTER_COMMAND = "reg";
|
||||||
|
const CLEAR_COMMAND = "cls";
|
||||||
|
const HELP_COMMAND = "?";
|
||||||
|
// DESCRIPTION
|
||||||
|
const PROMPT_DESCRIPTION = "Change the prompt between Long-mode or Short-mode.";
|
||||||
|
const REGISTER_DESCRIPTION = "Print the current values for Registers.";
|
||||||
|
const SEGMENT_REGISTER_DESCRIPTION = "Print the current values for Segment Registers.";
|
||||||
|
const CLEAR_DESCRIPTION = "Clears the screen.";
|
||||||
|
// USAGE
|
||||||
|
const PROMPT_USAGE = "prompt {short|long}";
|
||||||
|
const REGISTER_USAGE = "reg";
|
||||||
|
const SEGMENT_REGISTER_USAGE = "sr";
|
||||||
|
const HELP_USAGE = "?";
|
||||||
|
const CLEAR_USAGE = "cls";
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var memory = mem.Memory.init();
|
var memory = mem.Memory.init();
|
||||||
var cpu = cpu_.Cpu.init();
|
var cpu = cpu_.Cpu.init();
|
||||||
|
@ -58,7 +80,11 @@ pub fn main() !void {
|
||||||
try handleSegmentRegisterCommand(&cpu, &it);
|
try handleSegmentRegisterCommand(&cpu, &it);
|
||||||
} else if (std.mem.eql(u8, first_argument, "prompt")) {
|
} else if (std.mem.eql(u8, first_argument, "prompt")) {
|
||||||
try handlePromptCommand(&it);
|
try handlePromptCommand(&it);
|
||||||
} else if (std.mem.eql(u8, first_argument, "?")) {}
|
} else if (std.mem.eql(u8, first_argument, "?")) {
|
||||||
|
try handleHelpCommand(&it);
|
||||||
|
} else if (std.mem.eql(u8, first_argument, CLEAR_COMMAND)) {
|
||||||
|
try handleClearScreenCommand(&it);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// noop
|
// noop
|
||||||
}
|
}
|
||||||
|
@ -113,6 +139,28 @@ fn printSr(cpu: *cpu_.Cpu) void {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handleHelpCommand(it: anytype) !void {
|
||||||
|
if (it.next()) |_| {
|
||||||
|
std.debug.print(
|
||||||
|
\\{s}. {s}
|
||||||
|
\\
|
||||||
|
, .{ TOO_MANY_ARGUMENTS, HELP_USAGE });
|
||||||
|
} else {
|
||||||
|
std.debug.print(
|
||||||
|
\\{s: <20} {s}
|
||||||
|
\\{s: <20} {s}
|
||||||
|
\\{s: <20} {s}
|
||||||
|
\\{s: <20} {s}
|
||||||
|
\\
|
||||||
|
, .{
|
||||||
|
PROMPT_USAGE, PROMPT_DESCRIPTION,
|
||||||
|
REGISTER_USAGE, REGISTER_DESCRIPTION,
|
||||||
|
SEGMENT_REGISTER_USAGE, SEGMENT_REGISTER_DESCRIPTION,
|
||||||
|
CLEAR_USAGE, CLEAR_DESCRIPTION,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn handlePromptCommand(it: anytype) !void {
|
fn handlePromptCommand(it: anytype) !void {
|
||||||
if (it.next()) |next_argument| {
|
if (it.next()) |next_argument| {
|
||||||
if (std.mem.eql(u8, next_argument, "long")) {
|
if (std.mem.eql(u8, next_argument, "long")) {
|
||||||
|
@ -121,22 +169,42 @@ fn handlePromptCommand(it: anytype) !void {
|
||||||
long_prompt = false;
|
long_prompt = false;
|
||||||
} else {
|
} else {
|
||||||
std.debug.print(
|
std.debug.print(
|
||||||
\\Invalid argument. Usage: prompt <long|short>
|
\\{s}, usage: {s}
|
||||||
\\
|
\\
|
||||||
, .{});
|
, .{ WRONG_ARGUMENT, PROMPT_USAGE });
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
std.debug.print(
|
std.debug.print(
|
||||||
\\Invalid number of arguments. Usage: prompt <long|short>
|
\\{s}, usage: {s}
|
||||||
\\
|
\\
|
||||||
, .{});
|
, .{ TOO_MANY_ARGUMENTS, PROMPT_USAGE });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handleRegisterCommand(cpu: *cpu_.Cpu, it: anytype) !void {
|
||||||
|
_ = cpu;
|
||||||
|
_ = it;
|
||||||
|
}
|
||||||
|
|
||||||
fn handleSegmentRegisterCommand(cpu: *cpu_.Cpu, it: anytype) !void {
|
fn handleSegmentRegisterCommand(cpu: *cpu_.Cpu, it: anytype) !void {
|
||||||
if (it.next()) |_| {
|
if (it.next()) |_| {
|
||||||
std.debug.print("Wrong number of arguments.\n", .{});
|
std.debug.print(
|
||||||
|
\\{s}, usage: {s}
|
||||||
|
\\
|
||||||
|
, .{ TOO_MANY_ARGUMENTS, SEGMENT_REGISTER_USAGE });
|
||||||
} else {
|
} else {
|
||||||
printSr(cpu);
|
printSr(cpu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handleClearScreenCommand(it: anytype) !void {
|
||||||
|
var output = std.io.getStdOut().writer();
|
||||||
|
if (it.next()) |_| {
|
||||||
|
std.debug.print(
|
||||||
|
\\{s}, usage: {s}
|
||||||
|
\\
|
||||||
|
, .{ TOO_MANY_ARGUMENTS, CLEAR_USAGE });
|
||||||
|
} else {
|
||||||
|
try output.writeAll("\x1B[2J\x1B[H");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue