Skip to content
Snippets Groups Projects
Commit bbf2753e authored by Leo's avatar Leo
Browse files

fix: minor scoping for call expressions + new native functions for Strings

parent 4d728917
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,7 @@
<configuration default="false" name="Styve" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Styve" />
<module name="styve" />
<option name="PROGRAM_PARAMETERS" value="program_example.styve" />
<option name="PROGRAM_PARAMETERS" value="program_example.styve -ni" />
<method v="2">
<option name="Make" enabled="true" />
</method>
......
......@@ -53,16 +53,28 @@ Arne Arne Styve AddressBook Arne 8=
=D
=D
Arne Arne Styve Arne Arne formatPhone(phoneNumber) 8=
Arne? (phoneNumber Arne Styve starterMed("+")) 8=
phoneNumber Arne phoneNumber Arne Styve hentUtDel(1) Styve
=D Styve
Arne? (phoneNumber Arne Styve starterMed("47") Team phoneNumber Arne Styve lengde Samesies 10) 8=
phoneNumber Arne phoneNumber Arne Styve hentUtDel(2) Styve
=D Styve? Arne? (phoneNumber Arne Styve starterMed("0047")) 8=
phoneNumber Arne phoneNumber Arne Styve hentUtDel(4) Styve
=D
Arne Styve Styve "+47 " + phoneNumber Arne Styve hentUtDel(0, 3) + " " + phoneNumber Arne Styve hentUtDel(3, 5) + " " + phoneNumber Arne Styve hentUtDel(5) Styve
=D
--- Add some people to the address book
AddressBook Arne Styve addPerson(8=
fullName Kanban "Arne Styve",
phoneNumber Kanban "12345678"
phoneNumber Kanban "4712345678"
=D) Styve
AddressBook Arne Styve addPerson(8=
fullName Kanban "John Doe",
phoneNumber Kanban "87654321"
phoneNumber Kanban "+4787654321"
=D) Styve
AddressBook Arne Styve addPerson(8=
......@@ -72,7 +84,7 @@ AddressBook Arne Styve addPerson(8=
AddressBook Arne Styve addPerson(8=
fullName Kanban "Ola Nordmann",
phoneNumber Kanban "67812345"
phoneNumber Kanban "004767812345"
=D) Styve
......@@ -80,10 +92,15 @@ AddressBook Arne Styve addPerson(8=
STANDUP("Number of contacts:", AddressBook Arne Styve getNumberOfContacts()) Styve
--- Get a person by position
STANDUP("Person at position 0:", AddressBook Arne Styve getPerson(0)) Styve
STANDUP("Person at position -1:", AddressBook Arne Styve getPerson(-1)) Styve
Arne Arne Styve Arne Arne getPersonAtPosition(position) 8=
Arne Arne Styve person Arne AddressBook Arne Styve getPerson(position) Styve
AddressBook Arne Styve persons Arne Styve kaffePause() Styve
STANDUP("Person at position Center:", AddressBook Arne Styve getPerson(AddressBook Arne Styve persons Arne Styve antall / 2), "\n") Styve
STANDUP("Person at position " + position + " => Name:", person Arne Styve fullName + ",", "Phone:", formatPhone(person Arne Styve phoneNumber)) Styve
=D Styve
getPersonAtPosition(0) Styve
getPersonAtPosition(-1) Styve
getPersonAtPosition(AddressBook Arne Styve persons Arne Styve antall / 2) Styve
--- Print all contacts
STANDUP("All contacts:") Styve
......
......@@ -552,7 +552,7 @@ public class Expressions {
List<RTValue> args = new ArrayList<>();
for (Stmt arg : call.getArguments()) {
args.add(Interpreter.evaluate(arg, callEnv, debug));
args.add(Interpreter.evaluate(arg, env, debug));
}
return runFunction(fn, args, callEnv, debug);
}
......
......@@ -101,11 +101,76 @@ public class RTStringVal extends RTNativeAbstractObject {
return new RTBooleanVal(this.value.toLowerCase().contains(((RTStringVal) other).getValue().toLowerCase()));
}
private RTValue subString(List<RTValue> args, Environment env) {
/* Signatures can be:
subString(start: number) -> string
subString(start: number, end: number) -> string
*/
if (args.size() < 1 || args.size() > 2) {
throw new RuntimeException("String.subString() expects 1 or 2 arguments, got " + args.size());
}
RTValue start = args.getFirst();
if (start.getType() != rtValueType.number) {
throw new RuntimeException("String.subString() expects a number argument, got " + start.getType());
}
if (args.size() == 1) {
return new RTStringVal(this.value.substring((int) ((RTNumericVal) start).getValue()));
}
RTValue end = args.get(1);
if (end.getType() != rtValueType.number) {
throw new RuntimeException("String.subString() expects a number argument, got " + end.getType());
}
return new RTStringVal(this.value.substring((int) ((RTNumericVal) start).getValue(), (int) ((RTNumericVal) end).getValue()));
}
private RTValue replace(List<RTValue> args, Environment env) {
if (args.size() != 2) {
throw new RuntimeException("String.replace() expects 2 arguments, got " + args.size());
}
RTValue oldVal = args.getFirst();
RTValue newVal = args.get(1);
if (oldVal.getType() != rtValueType.string || newVal.getType() != rtValueType.string) {
throw new RuntimeException("String.replace() expects string arguments, got " + oldVal.getType() + " and " + newVal.getType());
}
return new RTStringVal(this.value.replace(((RTStringVal) oldVal).getValue(), ((RTStringVal) newVal).getValue()));
}
private RTValue replaceAll(List<RTValue> args, Environment env) {
if (args.size() != 2) {
throw new RuntimeException("String.replaceAll() expects 2 arguments, got " + args.size());
}
RTValue oldVal = args.getFirst();
RTValue newVal = args.get(1);
if (oldVal.getType() != rtValueType.string || newVal.getType() != rtValueType.string) {
throw new RuntimeException("String.replaceAll() expects string arguments, got " + oldVal.getType() + " and " + newVal.getType());
}
return new RTStringVal(this.value.replaceAll(((RTStringVal) oldVal).getValue(), ((RTStringVal) newVal).getValue()));
}
private RTValue toLowerCase(List<RTValue> args, Environment env) {
if (args.size() != 0) {
throw new RuntimeException("String.toLowerCase() expects 0 arguments, got " + args.size());
}
return new RTStringVal(this.value.toLowerCase());
}
private RTValue toUpperCase(List<RTValue> args, Environment env) {
if (args.size() != 0) {
throw new RuntimeException("String.toUpperCase() expects 0 arguments, got " + args.size());
}
return new RTStringVal(this.value.toUpperCase());
}
private void setupBuiltInMethods() {
this.putBuiltInMethod("leggTil", new RTNativeFunctionCall(this::concat));
this.putBuiltInMethod("starterMed", new RTNativeFunctionCall(this::startsWith));
this.putBuiltInMethod("inneholder", new RTNativeFunctionCall(this::contains));
this.putBuiltInMethod("inneholderAltSmått", new RTNativeFunctionCall(this::icontains));
this.putBuiltInMethod("hentUtDel", new RTNativeFunctionCall(this::subString));
this.putBuiltInMethod("erstatt", new RTNativeFunctionCall(this::replace));
this.putBuiltInMethod("erstattAlt", new RTNativeFunctionCall(this::replaceAll));
this.putBuiltInMethod("tilSmåBokstaver", new RTNativeFunctionCall(this::toLowerCase));
this.putBuiltInMethod("tilStoreBokstaver", new RTNativeFunctionCall(this::toUpperCase));
}
private void setupBuiltInProperties() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment