Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

Added Text.Append and Text.GetLength functions #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion build/strings/en/documentation.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,13 @@
"Stack_GetCount": "Gets the count of items in the specified stack.",
"Stack_GetCount_StackName": "The name of the stack.",
"Stack_PopValue": "Pops a value from the specified stack.",
"Stack_PopValue_StackName": "The name of the stack.",
"Stack_PopValue_StackName": "The name of the stack.",
"Text": "The Text object provides helpful operations for working with Text.",
"Text_Append": "Appends two text inputs and returns the result as another text. This operation is particularly useful when dealing with unknown text in variables which could accidentally be treated as numbers and get added, instead of getting appended.",
"Text_Append_Text1" : "First part of the text to be appended.",
"Text_Append_Text2" : "Second part of the text to be appended.",
"Text_GetLength" : "Gets the length of the given text.",
"Text_GetLength_Text" : "The text whose length is needed.",
"TextWindow": "The TextWindow provides text-related input and output functionalities. For example using this class, it is possible to write or read some text or number to and from the text-based text window.",
"TextWindow_Read": "Reads a line of text from the text window. Returns the string entered by the user.",
"TextWindow_ReadNumber": "Reads a number from the text window. Returns the number entered by the user.",
Expand Down
12 changes: 12 additions & 0 deletions src/compiler/runtime/libraries-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ export class LibrariesMetadata {
// No Events
});

public readonly Text: TypeMetadata = new TypeMetadata("Text",
{
Append: new MethodMetadata("Text", "Append", true, ["Text1", "Text2"]),
GetLength: new MethodMetadata("Text", "GetLength", true, ["Text"])
},
{
// No Properties
},
{
// No Events
});

public readonly TextWindow: TypeMetadata = new TypeMetadata("TextWindow",
{
Read: new MethodMetadata("TextWindow", "Read", true, []),
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/runtime/libraries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { LibrariesMetadata } from "./libraries-metadata";
import { MathLibrary } from "./libraries/math";
import { CompilerRange } from "../syntax/ranges";
import { ShapesLibrary } from "./libraries/shapes";
import { TextLibrary } from "./libraries/text";

export interface LibraryTypeInstance {
readonly methods: { readonly [name: string]: LibraryMethodInstance };
Expand Down Expand Up @@ -42,6 +43,7 @@ export class RuntimeLibraries {
public readonly Program: ProgramLibrary = new ProgramLibrary();
public readonly Shapes: ShapesLibrary = new ShapesLibrary();
public readonly Stack: StackLibrary = new StackLibrary();
public readonly Text: TextLibrary = new TextLibrary();
public readonly TextWindow: TextWindowLibrary = new TextWindowLibrary();
// TODO: public readonly Turtle: TurtleLibrary = new TurtleLibrary();
}
26 changes: 26 additions & 0 deletions src/compiler/runtime/libraries/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { StringValue } from "../values/string-value";
import { NumberValue } from "../values/number-value";
import { ExecutionEngine } from "../../execution-engine";
import { LibraryMethodInstance, LibraryTypeInstance, LibraryPropertyInstance, LibraryEventInstance } from "../libraries";

export class TextLibrary implements LibraryTypeInstance {
private executeAppend(engine: ExecutionEngine): void {
const text2 = engine.popEvaluationStack();
const text1 = engine.popEvaluationStack();
engine.pushEvaluationStack(new StringValue(text1.toValueString() + text2.toValueString()));
}

private executeGetLength(engine: ExecutionEngine): void {
const text = engine.popEvaluationStack();
engine.pushEvaluationStack(new NumberValue(text.toValueString().length));
}

public readonly methods: { readonly [name: string]: LibraryMethodInstance } = {
Append: { execute: this.executeAppend.bind(this) },
GetLength: { execute: this.executeGetLength.bind(this) }
};

public readonly properties: { readonly [name: string]: LibraryPropertyInstance } = {};

public readonly events: { readonly [name: string]: LibraryEventInstance } = {};
}
6 changes: 6 additions & 0 deletions src/strings/documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ export module DocumentationResources {
export const Stack_GetCount_StackName = "The name of the stack.";
export const Stack_PopValue = "Pops a value from the specified stack.";
export const Stack_PopValue_StackName = "The name of the stack.";
export const Text = "The Text object provides helpful operations for working with Text.";
export const Text_Append = "Appends two text inputs and returns the result as another text. This operation is particularly useful when dealing with unknown text in variables which could accidentally be treated as numbers and get added, instead of getting appended.";
export const Text_Append_Text1 = "First part of the text to be appended.";
export const Text_Append_Text2 = "Second part of the text to be appended.";
export const Text_GetLength = "Gets the length of the given text.";
export const Text_GetLength_Text = "The text whose length is needed.";
export const TextWindow = "The TextWindow provides text-related input and output functionalities. For example using this class, it is possible to write or read some text or number to and from the text-based text window.";
export const TextWindow_Read = "Reads a line of text from the text window. Returns the string entered by the user.";
export const TextWindow_ReadNumber = "Reads a number from the text window. Returns the number entered by the user.";
Expand Down
36 changes: 36 additions & 0 deletions tests/compiler/runtime/libraries/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import "jasmine";
import { verifyRuntimeResult } from "../../helpers";

describe("Compiler.Runtime.Libraries.Text", () => {
it("can return appended text", () => {
verifyRuntimeResult(`
x = 1
y = "value"
TextWindow.WriteLine(Text.Append(x, y))
TextWindow.WriteLine(Text.Append(x, "test"))`,
[],
[
"1value",
"1test"
]);
});

it("can can get length of the string", () => {
verifyRuntimeResult(`
str1 = ""
str2 = "test"
TextWindow.WriteLine(Text.GetLength(0))
TextWindow.WriteLine(Text.GetLength("a"))
TextWindow.WriteLine(Text.GetLength(x)) ' Nothing
TextWindow.WriteLine(Text.GetLength(str1))
TextWindow.WriteLine(Text.GetLength(str2))`,
[],
[
"1",
"1",
"0",
"0",
"4"
]);
});
});
1 change: 1 addition & 0 deletions tests/compiler/tests-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import "./runtime/libraries/clock";
import "./runtime/libraries/math";
import "./runtime/libraries/program";
import "./runtime/libraries/stack";
import "./runtime/libraries/text";
import "./runtime/libraries/text-window";

import "./runtime/statements/for-loop";
Expand Down