Skip to content

support variable parameters and printf() sprintf()#15

Merged
crossoverJie merged 6 commits intomainfrom
feature/new-internal-function
Sep 16, 2022
Merged

support variable parameters and printf() sprintf()#15
crossoverJie merged 6 commits intomainfrom
feature/new-internal-function

Conversation

@crossoverJie
Copy link
Owner

@crossoverJie crossoverJie commented Sep 16, 2022

New Built-in function:

//formats according to a format specifier and writes to standard output.
printf(string format, any ...a){}

//formats according to a format specifier and returns the resulting string.
string sprintf(string format, any ...a){}

Example:

printf("hello %s ","123");
printf("hello-%s-%s ","123","abc");
printf("hello-%s-%d ","123",123);
string format = "this is %s ";
printf(format, "gscript");

string s = sprintf("nice to meet %s", "you");
println(s);
assertEqual(s,"nice to meet you");

And support variable parameters.

int add(string s, int ...num){
	println(s);
	int sum = 0;
	for(int i=0;i<len(num);i++){
		int v = num[i];
		sum = sum+v;
	}
	return sum;
}
int x = add("abc", 1,2,3,4);
println(x);
assertEqual(x, 10);

Optimized append() function:

// Before
int[] a={1,2,3};
println(a);
println();
a = append(a,4);
println(a);
// Output: [1 2 3 4]

// Now
int[] a={1,2,3};
println(a);
println();
append(a,4);
int b = a[3];
assertEqual(4, b);
println(a);
// Output: [1 2 3 4]

@crossoverJie crossoverJie merged commit b3eed71 into main Sep 16, 2022
@crossoverJie crossoverJie deleted the feature/new-internal-function branch September 16, 2022 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant