Skip to content

Commit

Permalink
Merge pull request google#15 from HighSchoolHacking/command-semicolons
Browse files Browse the repository at this point in the history
Command semicolons
  • Loading branch information
Josh Goldberg committed Apr 11, 2016
2 parents 065f221 + 92cefeb commit 065f36b
Show file tree
Hide file tree
Showing 83 changed files with 382 additions and 342 deletions.
7 changes: 4 additions & 3 deletions Source/Commands/ArrayInitializeCommand.ts
@@ -1,4 +1,5 @@
/// <reference path="../Languages/Language.ts" />
/// <reference path="LineResults.ts" />
/// <reference path="Command.ts" />

namespace GLS.Commands {
Expand All @@ -16,7 +17,7 @@ namespace GLS.Commands {
* @returns Line(s) of code in the language.
* @remarks Usage: (type[, item, ...]).
*/
public render(parameters: string[]): CommandResult[] {
public render(parameters: string[]): LineResults {
this.requireParametersLengthMinimum(parameters, 1);

let typeName: string = this.context.convertCommon("type", parameters[1]),
Expand All @@ -29,7 +30,7 @@ namespace GLS.Commands {
if (this.language.properties.arrays.initializeByType) {
if (parameters.length === 2) {
output += typeName + "[0]";
return [new CommandResult(output, 0)];
return LineResults.newSingleLine(output, false);
}

output += this.context.convertCommon("type", typeName + "[]");
Expand All @@ -49,7 +50,7 @@ namespace GLS.Commands {
output += "]";
}

return [new CommandResult(output, 0)];
return LineResults.newSingleLine(output, false);
}
}
}
7 changes: 4 additions & 3 deletions Source/Commands/BlockEndCommand.ts
@@ -1,5 +1,6 @@
/// <reference path="../Languages/Language.ts" />
/// <reference path="Command.ts" />
/// <reference path="LineResults.ts" />

namespace GLS.Commands {
"use strict";
Expand All @@ -15,16 +16,16 @@ namespace GLS.Commands {
* @returns Line(s) of code in the language.
* @remarks Usage: ().
*/
public render(parameters: string[]): CommandResult[] {
public render(parameters: string[]): LineResults {
this.requireParametersLength(parameters, 0);

let ender: string = this.language.properties.conditionals.end;

if (ender === "\0") {
return [];
return LineResults.newBlockLine("\0", -1);
}

return [new CommandResult(ender, -1)];
return LineResults.newBlockLine(ender, -1);
}
}
}
6 changes: 3 additions & 3 deletions Source/Commands/BreakCommand.ts
@@ -1,5 +1,6 @@
/// <reference path="../Languages/Language.ts" />
/// <reference path="Command.ts" />
/// <reference path="LineResults.ts" />

namespace GLS.Commands {
"use strict";
Expand All @@ -15,13 +16,12 @@ namespace GLS.Commands {
* @returns Line(s) of code in the language.
* @remarks Usage: ().
*/
public render(parameters: string[]): CommandResult[] {
public render(parameters: string[]): LineResults {
this.requireParametersLength(parameters, 0);

let output: string = this.language.properties.loops.break;
output += this.language.properties.style.semicolon;

return [new CommandResult(output, 0)];
return LineResults.newSingleLine(output, true);
}
}
}
118 changes: 14 additions & 104 deletions Source/Commands/Command.ts
@@ -1,5 +1,6 @@
/// <reference path="../Languages/Language.ts" />
/// <reference path="../ConversionContext.ts" />
/// <reference path="LineResults.ts" />

namespace GLS.Commands {
"use strict";
Expand All @@ -18,6 +19,11 @@ namespace GLS.Commands {
*/
protected language: Languages.Language;

/**
* Whether this command'slines should end with a semicolon.
*/
protected addsSemicolon: boolean;

/**
* Initializes a new instance of the Command class.
*
Expand All @@ -34,7 +40,14 @@ namespace GLS.Commands {
* @param parameters The command's name, followed by any parameters.
* @returns Line(s) of code in the language.
*/
public abstract render(parameters: string[]): CommandResult[];
public abstract render(parameters: string[]): LineResults;

/**
* @returns Whether this command's lines should end with a semicolon.
*/
public getAddsSemicolon(): boolean {
return this.addsSemicolon;
}

/**
* Adds a portion of raw syntax that may contain endlines.
Expand Down Expand Up @@ -71,9 +84,6 @@ namespace GLS.Commands {
currentLine.text = extra.substring(currentIndex + 1);
}

if (this.language.properties.general.name === "TypeScript") {
console.log("Giving", indentation, "to", lines.length - 1, lines[lines.length - 1]);
}
lines[lines.length - 1].indentation = indentation;
}

Expand Down Expand Up @@ -147,104 +157,4 @@ namespace GLS.Commands {
}
}
}

/**
* A command for performing a native call, such as Array::push.
*/
export abstract class NativeCallCommand extends Command {
/**
* Metadata on how to perform the native call.
*/
protected nativeCallProperties: Languages.Properties.NativeCallProperties;

/**
* Initializes a new instance of the Command class.
*
* @param context The driving context for converting the command.
*/
constructor(context: ConversionContext) {
super(context);

this.nativeCallProperties = this.retrieveNativeCallProperties();
}

/**
* Renders the command for a language with the given parameters.
*
* @param parameters The command's name, followed by any number of
* items to initialize in the Array.
* @returns Line(s) of code in the language.
* @remarks Usage: (name[, parameters, ...]).
*/
public render(parameters: string[]): CommandResult[] {
if (this.nativeCallProperties.asStatic) {
return this.renderAsStatic(parameters);
}

return this.renderAsMember(parameters);
}

/**
* Renders the native call as a static.
*
* @param parameters The command's name, followed by any number of
* items to initialize in the Array.
* @returns Line(s) of code in the language.
* @remarks Usage: (name[, parameters, ...]).
*/
private renderAsStatic(parameters: string[]): CommandResult[] {
this.requireParametersLengthMinimum(parameters, 1);

let result: string = "";

result += this.nativeCallProperties.name;
result += "(" + parameters[1];

for (let i: number = 2; i < parameters.length; i += 1) {
result += ", " + parameters[i];
}

result += ")";

return [new CommandResult(result, 0)];
}

/**
* Renders the native call as a member.
*
* @param parameters The command's name, followed by any number of
* items to initialize in the Array.
* @returns Line(s) of code in the language.
* @remarks Usage: (name[, parameters, ...]).
*/
private renderAsMember(parameters: string[]): CommandResult[] {
this.requireParametersLengthMinimum(parameters, 1);

let result: string = "";

result += parameters[1] + ".";
result += this.nativeCallProperties.name;

if (this.nativeCallProperties.asFunction) {
result += "(";

if (parameters.length >= 2) {
result += parameters[2];

for (let i: number = 3; i < parameters.length; i += 1) {
result += ", " + parameters[i];
}
}

result += ")";
}

return [new CommandResult(result, 0)];
}

/**
* @returns Metadata on how to perform the native call.
*/
protected abstract retrieveNativeCallProperties(): Languages.Properties.NativeCallProperties;
}
}
4 changes: 1 addition & 3 deletions Source/Commands/CommandsBag.ts
Expand Up @@ -36,7 +36,6 @@
/// <reference path="MainStartCommand.ts" />
/// <reference path="NotCommand.ts" />
/// <reference path="OperationCommand.ts" />
/// <reference path="OperationInlineCommand.ts" />
/// <reference path="OperatorCommand.ts" />
/// <reference path="ParenthesisCommand.ts" />
/// <reference path="PrintCommand.ts" />
Expand Down Expand Up @@ -104,7 +103,6 @@ namespace GLS.Commands {
"main start": new MainStartCommand(context),
"not": new NotCommand(context),
"operation": new OperationCommand(context),
"operation inline": new OperationInlineCommand(context),
"operator": new OperatorCommand(context),
"parenthesis": new ParenthesisCommand(context),
"print": new PrintCommand(context),
Expand All @@ -128,7 +126,7 @@ namespace GLS.Commands {
* @param command A command name, followed by parameters for it.
* @returns Line(s) of code in the language.
*/
renderCommand(parameters: string[]): CommandResult[] {
renderCommand(parameters: string[]): Commands.LineResults {
if (!this.commands.hasOwnProperty(parameters[0])) {
throw new Error("Unknown command requested: '" + parameters[0] + "'");
}
Expand Down
12 changes: 6 additions & 6 deletions Source/Commands/CommentBlockCommand.ts
Expand Up @@ -15,14 +15,14 @@ namespace GLS.Commands {
* @returns Line(s) of code in the language.
* @remarks Usage: (contents, ...).
*/
public render(parameters: string[]): CommandResult[] {
let result: string = "";
public render(parameters: string[]): LineResults {
let output: string = "";

result += this.language.properties.comments.blockLineLeft;
result += parameters.slice(1).join(" ");
result += this.language.properties.comments.blockLineRight;
output += this.language.properties.comments.blockLineLeft;
output += parameters.slice(1).join(" ");
output += this.language.properties.comments.blockLineRight;

return [new CommandResult(result, 0)];
return LineResults.newSingleLine(output, false);
}
}
}
4 changes: 2 additions & 2 deletions Source/Commands/CommentBlockEndCommand.ts
Expand Up @@ -15,8 +15,8 @@ namespace GLS.Commands {
* @returns Line(s) of code in the language.
* @remarks Usage: ().
*/
public render(parameters: string[]): CommandResult[] {
return [new CommandResult(this.language.properties.comments.blockEnd, 0)];
public render(parameters: string[]): LineResults {
return LineResults.newSingleLine(this.language.properties.comments.blockEnd, false);
}
}
}
5 changes: 3 additions & 2 deletions Source/Commands/CommentBlockStartCommand.ts
@@ -1,5 +1,6 @@
/// <reference path="../Languages/Language.ts" />
/// <reference path="Command.ts" />
/// <reference path="LineResults.ts" />

namespace GLS.Commands {
"use strict";
Expand All @@ -15,8 +16,8 @@ namespace GLS.Commands {
* @returns Line(s) of code in the language.
* @remarks Usage: ().
*/
public render(parameters: string[]): CommandResult[] {
return [new CommandResult(this.language.properties.comments.blockStart, 0)];
public render(parameters: string[]): LineResults {
return LineResults.newSingleLine(this.language.properties.comments.blockStart, false);
}
}
}
5 changes: 3 additions & 2 deletions Source/Commands/CommentDocEndCommand.ts
@@ -1,5 +1,6 @@
/// <reference path="../Languages/Language.ts" />
/// <reference path="Command.ts" />
/// <reference path="LineResults.ts" />

namespace GLS.Commands {
"use strict";
Expand All @@ -15,8 +16,8 @@ namespace GLS.Commands {
* @returns Line(s) of code in the language.
* @remarks Usage: ().
*/
public render(parameters: string[]): CommandResult[] {
return [new CommandResult(this.language.properties.comments.docEnd, 0)];
public render(parameters: string[]): LineResults {
return LineResults.newSingleLine(this.language.properties.comments.docEnd, false);
}
}
}
5 changes: 3 additions & 2 deletions Source/Commands/CommentDocStartCommand.ts
@@ -1,5 +1,6 @@
/// <reference path="../Languages/Language.ts" />
/// <reference path="Command.ts" />
/// <reference path="LineResults.ts" />

namespace GLS.Commands {
"use strict";
Expand All @@ -15,8 +16,8 @@ namespace GLS.Commands {
* @returns Line(s) of code in the language.
* @remarks Usage: ().
*/
public render(parameters: string[]): CommandResult[] {
return [new CommandResult(this.language.properties.comments.docStart, 0)];
public render(parameters: string[]): LineResults {
return LineResults.newSingleLine(this.language.properties.comments.docStart, false);
}
}
}

0 comments on commit 065f36b

Please sign in to comment.