From 6559f386d299a3f6d2a4a21f8de838bd6f91e842 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Fri, 1 Apr 2016 22:32:33 -0700 Subject: [PATCH] Move most of core stuff --- libs/microbit/core.cpp | 139 +++++++++++++++++++++++++++++++++++++++ libs/microbit/core.d.ts | 116 -------------------------------- libs/microbit/game.ts | 16 ++--- libs/microbit/kind.json | 1 + libs/microbit/mbit.ts | 48 ++++++++++++-- libs/microbit/shims.d.ts | 84 +++++++++++++++++++++++ 6 files changed, 274 insertions(+), 130 deletions(-) create mode 100644 libs/microbit/core.cpp diff --git a/libs/microbit/core.cpp b/libs/microbit/core.cpp new file mode 100644 index 00000000..c78fca9d --- /dev/null +++ b/libs/microbit/core.cpp @@ -0,0 +1,139 @@ +#include "ksbit.h" +#include + +namespace StringMethods { + + /** + * Returns the character at the specified index. + * @param pos The zero-based index of the desired character. + */ + //% + StringData *charAt(StringData *s, int pos) { + return ManagedString((char)ManagedString(s).charAt(pos)).leakData(); + } + + /** + * Returns the Unicode value of the character at the specified location. + * @param index The zero-based index of the desired character. If there is no character at the specified index, NaN is returned. + */ + //% + int charCodeAt(StringData *s, int index) { + return ManagedString(s).charAt(index); + } + + /** + * Returns a string that contains the concatenation of two or more strings. + * @param other The string to append to the end of the string. + */ + //% + StringData *concat(StringData *s, StringData *other) { + ManagedString a(s), b(other); + return (a + b).leakData(); + } + + /** + * Determines whether relative order of two strings (in ASCII encoding). + * @param that String to compare to target string + */ + //% + int compare(StringData *s, StringData *that) { + return strcmp(s->data, that->data); + } + + /** Returns the length of a String object. */ + //% property + int length(StringData *s) { + return s->len; + } +} + + +namespace BooleanMethods { + // Cache the string literals "true" and "false" when used. + // Note that the representation of booleans stays the usual C-one. + + static const char sTrue[] __attribute__ ((aligned (4))) = "\xff\xff\x04\x00" "true\0"; + static const char sFalse[] __attribute__ ((aligned (4))) = "\xff\xff\x05\x00" "false\0"; + + /** + * Returns a string representation of an object. + */ + //% + StringData* toString(bool v) + { + if (v) { + return (StringData*)(void*)sTrue; + } else { + return (StringData*)(void*)sFalse; + } + } +} + +namespace String { + /** + * Make a string from the given ASCII character code. + */ + //% + StringData *fromCharCode(int code) + { + return ManagedString((char)code).leakData(); + } +} + +namespace NumberMethods { + /** + * Returns a string representation of a number. + */ + //% + StringData* toString(int n) + { + return ManagedString(n).leakData(); + } +} + +namespace Math { + /** + * Returns the value of a base expression taken to a specified power. + * @param x The base value of the expression. + * @param y The exponent value of the expression. + */ + //% + int pow(int x, int y) + { + if (y < 0) + return 0; + int r = 1; + while (y) { + if (y & 1) + r *= x; + y >>= 1; + x *= x; + } + return r; + } + + /** + * Returns a pseudorandom number between 0 and `max`. + */ + //% + int random(int max) { + if (max == INT_MIN) + return -uBit.random(INT_MAX); + else if (max < 0) + return -uBit.random(-max); + else if (max == 0) + return 0; + else + return uBit.random(max); + } + + /** + * Returns the square root of a number. + * @param x A numeric expression. + */ + //% + int sqrt(int x) + { + return ::sqrt(x); + } +} diff --git a/libs/microbit/core.d.ts b/libs/microbit/core.d.ts index e81b586d..f1990c39 100644 --- a/libs/microbit/core.d.ts +++ b/libs/microbit/core.d.ts @@ -78,75 +78,6 @@ interface Array { interface String { - - /** - * Returns the character at the specified index. - * @param pos The zero-based index of the desired character. - */ - //% shim=string::at - charAt(pos: number): string; - - /** - * Returns the Unicode value of the character at the specified location. - * @param index The zero-based index of the desired character. If there is no character at the specified index, NaN is returned. - */ - //% shim=string::code_at - charCodeAt(index: number): number; - - /** - * Returns a string that contains the concatenation of two or more strings. - * @param strings The strings to append to the end of the string. - */ - //% shim=string::concat - concat(other: string): string; - - /** - * Returns the position of the first occurrence of a substring. - * @param searchString The substring to search for in the string - * @param position The index at which to begin searching the String object. If omitted, search starts at the beginning of the string. - */ - indexOf(searchString: string, position?: number): number; - - /** - * Returns the last occurrence of a substring in the string. - * @param searchString The substring to search for. - * @param position The index at which to begin searching. If omitted, the search begins at the end of the string. - */ - lastIndexOf(searchString: string, position?: number): number; - - /** - * Determines whether two strings are equivalent in the current locale. - * @param that String to compare to target string - */ - localeCompare(that: string): number; - - /** - * Returns a section of a string. - * @param start The index to the beginning of the specified portion of stringObj. - * @param end The index to the end of the specified portion of stringObj. The substring includes the characters up to, but not including, the character indicated by end. - * If this value is not specified, the substring continues to the end of stringObj. - */ - slice(start?: number, end?: number): string; - - /** - * Returns the substring at the specified location within a String object. - * @param start The zero-based index number indicating the beginning of the substring. - * @param end Zero-based index number indicating the end of the substring. The substring includes the characters up to, but not including, the character indicated by end. - * If end is omitted, the characters from start through the end of the original string are returned. - */ - substring(start: number, end?: number): string; - - /** Converts all the alphabetic characters in a string to lowercase. */ - toLowerCase(): string; - - /** Converts all the alphabetic characters in a string to uppercase. */ - toUpperCase(): string; - - /** Returns the length of a String object. */ - //% shim=string::count - length: number; - - //% shim=string::at [index: number]: string; } @@ -187,50 +118,3 @@ interface Number { toString(): string; } -declare namespace Math { - /** - * Returns the absolute value of a number (the value without regard to whether it is positive or negative). - * For example, the absolute value of -5 is the same as the absolute value of 5. - * @param x A numeric expression for which the absolute value is needed. - */ - //% shim=math::abs - export function abs(x: number): number; - - /** - * Returns the sign of the x, indicating whether x is positive, negative or zero. - * @param x The numeric expression to test - */ - //% shim=math::sign - export function sign(x: number): number; - - /** - * Returns the larger of two supplied numeric expressions. - */ - //% shim=math::max - export function max(a:number, b:number): number; - - /** - * Returns the smaller of two supplied numeric expressions. - */ - //% shim=math::min - export function min(a:number, b:number): number; - - /** - * Returns the value of a base expression taken to a specified power. - * @param x The base value of the expression. - * @param y The exponent value of the expression. - */ - //% shim=math::pow - export function pow(x: number, y: number): number; - - /** Returns a pseudorandom number between 0 and `max`. */ - //% shim=math::random - export function random(max:number): number; - - /** - * Returns the square root of a number. - * @param x A numeric expression. - */ - //% shim=math::sqrt - export function sqrt(x: number): number; -} diff --git a/libs/microbit/game.ts b/libs/microbit/game.ts index 17ece0a1..6af2db6a 100644 --- a/libs/microbit/game.ts +++ b/libs/microbit/game.ts @@ -284,8 +284,8 @@ namespace game { private _blink: number; constructor(x: number, y: number) { - this._x = math.clamp(0, 4, x); - this._y = math.clamp(0, 4, y); + this._x = Math.clamp(0, 4, x); + this._y = Math.clamp(0, 4, y); this._dir = 90; this._brightness = 255; init(); @@ -322,8 +322,8 @@ namespace game { this._x = this._x - leds; this._y = this._y + leds; } - this._x = math.clamp(0, 4, this._x); - this._y = math.clamp(0, 4, this._y); + this._x = Math.clamp(0, 4, this._x); + this._y = Math.clamp(0, 4, this._y); plot(); } @@ -336,8 +336,8 @@ namespace game { public goTo(x: number, y: number): void { this._x = x; this._y = y; - this._x = math.clamp(0, 4, this._x); - this._y = math.clamp(0, 4, this._y); + this._x = Math.clamp(0, 4, this._x); + this._y = Math.clamp(0, 4, this._y); plot(); } @@ -594,7 +594,7 @@ namespace game { * @param brightness TODO */ public setBrightness(brightness: number): void { - this._brightness = math.clamp(0, 255, brightness); + this._brightness = Math.clamp(0, 255, brightness); plot(); } @@ -639,7 +639,7 @@ namespace game { * @param ms TODO */ public setBlink(ms: number): void { - this._blink = math.clamp(0, 10000, ms); + this._blink = Math.clamp(0, 10000, ms); } /** diff --git a/libs/microbit/kind.json b/libs/microbit/kind.json index d2e363e3..ef6501ea 100644 --- a/libs/microbit/kind.json +++ b/libs/microbit/kind.json @@ -9,6 +9,7 @@ "shims.d.ts", "core.d.ts", "ksbit.h", + "core.cpp", "mbit.ts", "images.cpp", "basic.cpp", diff --git a/libs/microbit/mbit.ts b/libs/microbit/mbit.ts index 52f6b1c9..c21980e5 100644 --- a/libs/microbit/mbit.ts +++ b/libs/microbit/mbit.ts @@ -13,16 +13,52 @@ namespace helpers { namespace console { export function log(msg: string) { - writeString(msg); - writeString("\r\n"); + serial.writeString(msg); + serial.writeString("\r\n"); } - - //% shim=micro_bit::serialSendString - function writeString(text: string): void { } } -namespace math { +namespace Math { export function clamp(min: number, max:number, value:number): number { return Math.min(max, Math.max(min, value)); } + + /** + * Returns the absolute value of a number (the value without regard to whether it is positive or negative). + * For example, the absolute value of -5 is the same as the absolute value of 5. + * @param x A numeric expression for which the absolute value is needed. + */ + export function abs(x: number): number + { + return x < 0 ? -x : x; + } + + /** + * Returns the sign of the x, indicating whether x is positive, negative or zero. + * @param x The numeric expression to test + */ + export function sign(x: number): number + { + if (x == 0) return 0; + if (x > 0) return 1; + return -1; + } + + /** + * Returns the larger of two supplied numeric expressions. + */ + export function max(a:number, b:number): number + { + if (a >= b) return a; + return b; + } + + /** + * Returns the smaller of two supplied numeric expressions. + */ + export function min(a:number, b:number): number + { + if (a <= b) return a; + return b; + } } diff --git a/libs/microbit/shims.d.ts b/libs/microbit/shims.d.ts index 90180d17..fe0355f1 100644 --- a/libs/microbit/shims.d.ts +++ b/libs/microbit/shims.d.ts @@ -1,6 +1,90 @@ // Auto-generated. Do not edit. +declare interface String { + /** + * Returns the character at the specified index. + * @param pos The zero-based index of the desired character. + */ + //% shim=StringMethods::charAt + charAt(pos: number): string; + + /** + * Returns the Unicode value of the character at the specified location. + * @param index The zero-based index of the desired character. If there is no character at the specified index, NaN is returned. + */ + //% shim=StringMethods::charCodeAt + charCodeAt(index: number): number; + + /** + * Returns a string that contains the concatenation of two or more strings. + * @param other The string to append to the end of the string. + */ + //% shim=StringMethods::concat + concat(other: string): string; + + /** + * Determines whether relative order of two strings (in ASCII encoding). + * @param that String to compare to target string + */ + //% shim=StringMethods::compare + compare(that: string): number; + + /** Returns the length of a String object. */ + //% property shim=StringMethods::length + length(): number; +} + + +declare interface Boolean { + /** + * Returns a string representation of an object. + */ + //% shim=BooleanMethods::toString + toString(): string; +} +declare namespace String { + + /** + * Make a string from the given ASCII character code. + */ + //% shim=String::fromCharCode + function fromCharCode(code: number): string; +} + + +declare interface Number { + /** + * Returns a string representation of a number. + */ + //% shim=NumberMethods::toString + toString(): string; +} +declare namespace Math { + + /** + * Returns the value of a base expression taken to a specified power. + * @param x The base value of the expression. + * @param y The exponent value of the expression. + */ + //% shim=Math::pow + function pow(x: number, y: number): number; + + /** + * Returns a pseudorandom number between 0 and `max`. + */ + //% shim=Math::random + function random(max: number): number; + + /** + * Returns the square root of a number. + * @param x A numeric expression. + */ + //% shim=Math::sqrt + function sqrt(x: number): number; +} + + //% color=45 weight=31 declare namespace images {