Prep for moving common stuff out to main ks

This commit is contained in:
Michal Moskal 2016-04-04 20:28:08 -07:00
parent 46d42e5300
commit b8d5ec853e
4 changed files with 122 additions and 166 deletions

View File

@ -1,81 +1,31 @@
#include "ksbit.h"
#include <limits.h>
namespace StringMethods {
/**
* Returns the character at the specified index.
* @param pos The zero-based index of the desired character.
*/
namespace String_ {
//%
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;
}
}
int length(StringData *s) { return s->len; }
//%
bool bang(bool v) { return !v; }
}
namespace String_ {
/**
* Make a string from the given ASCII character code.
*/
//%
StringData *fromCharCode(int code)
{
@ -94,18 +44,35 @@ namespace String_ {
}
}
namespace NumberMethods {
/**
* Returns a string representation of a number.
*/
namespace Boolean_ {
// 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";
//%
StringData* toString(bool v)
{
if (v) {
return (StringData*)(void*)sTrue;
} else {
return (StringData*)(void*)sFalse;
}
}
//%
bool bang(bool v) { return !v; }
}
namespace Number_ {
//%
StringData* toString(int n)
{
return ManagedString(n).leakData();
}
}
namespace NumberImpl {
// +, - and friends are handled directly by assembly instructions
// The comparisons are here as they are more code-size efficient
@ -130,11 +97,6 @@ namespace NumberImpl {
}
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)
{
@ -150,9 +112,6 @@ namespace Math_ {
return r;
}
/**
* Returns a pseudorandom number between 0 and `max`.
*/
//%
int random(int max) {
if (max == INT_MIN)
@ -165,10 +124,6 @@ namespace Math_ {
return uBit.random(max);
}
/**
* Returns the square root of a number.
* @param x A numeric expression.
*/
//%
int sqrt(int x)
{
@ -176,7 +131,7 @@ namespace Math_ {
}
}
namespace ArrayImpl {
namespace Array_ {
//%
RefCollection *mk(uint32_t flags)
{

View File

@ -4,14 +4,14 @@ interface Array<T> {
/**
* Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.
*/
//% shim=ArrayImpl::length
//% shim=Array_::length
length: number;
/**
* Appends new elements to an array.
* @param items New elements of the Array.
*/
//% shim=ArrayImpl::push
//% shim=Array_::push
push(item: T): void;
/**
@ -41,11 +41,11 @@ interface Array<T> {
slice(start: number, end: number): T[];
/** Removes the first occurence of an object. Returns true if removed. */
//% shim=ArrayImpl::removeElement
//% shim=Array_::removeElement
removeElement(element:T) : boolean;
/** Removes the object at position index. */
//% shim=ArrayImpl::removeAt
//% shim=Array_::removeAt
removeAt(idx:number) : void;
@ -69,31 +69,110 @@ interface Array<T> {
* @param searchElement The value to locate in the array.
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
*/
//% shim=ArrayImpl::indexOf
//% shim=Array_::indexOf
indexOf(searchElement: T, fromIndex?: number): number;
[n: number]: T;
}
declare interface String {
/**
* Returns the character at the specified index.
* @param pos The zero-based index of the desired character.
*/
//% shim=String_::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=String_::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=String_::concat
concat(other: string): string;
/**
* Determines whether relative order of two strings (in ASCII encoding).
* @param that String to compare to target string
*/
//% shim=String_::compare
compare(that: string): number;
/** Returns the length of a String object. */
//% property shim=String_::length
length: number;
interface String {
[index: number]: string;
}
interface Buffer {
[index: number]: number;
}
/**
* Converts A string to an integer.
* @param s A string to convert into a number.
*/
//% shim=String_::toNumber
//% shim=String_::toNumber
declare function parseInt(s: string): number;
interface Object {}
interface Function {}
interface IArguments {}
interface RegExp {}
declare interface Boolean {
/**
* Returns a string representation of an object.
*/
//% shim=Boolean_::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=Number_::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;
}

View File

@ -53,3 +53,9 @@ namespace pins {
return 0;
}
}
interface Buffer {
[index: number]: number;
// rest defined in buffer.cpp
}

View File

@ -1,90 +1,6 @@
// 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 {