private void VerifyCount(SystemFunctionCall functionCall, int argCount) { if (functionCall.Args.Length != argCount) { throw new ParserException(functionCall.FirstToken, "Wrong number of args. Expected: " + argCount); } }
private void VerifyCountAtLeast(SystemFunctionCall functionCall, int minArgCount) { if (functionCall.Args.Length < minArgCount) { throw new ParserException(functionCall.FirstToken, "Not enough args. Expected at least " + minArgCount); } }
private void VerifyCount(SystemFunctionCall functionCall, int argCount1, params int[] orTheseArgCounts) { int count = functionCall.Args.Length; if (count == argCount1) { return; } foreach (int argCount in orTheseArgCounts) { if (argCount == count) { return; } } throw new ParserException(functionCall.FirstToken, "Wrong number of args."); }
private void TranslateSystemFunctionCall(List <string> output, SystemFunctionCall systemFunctionCall) { this.Platform.SystemFunctionTranslator.Translate(this.CurrentTabIndention, output, systemFunctionCall); }
public void Translate(string tab, List <string> output, SystemFunctionCall functionCall) { Expression[] args = functionCall.Args; string fullName = functionCall.Name; string name = fullName.Substring(1); if (name.StartsWith("_lib_")) { output.Add(functionCall.AssociatedLibrary.TranslateNativeInvocation(functionCall.FirstToken, this.Platform, fullName, args)); return; } switch (name) { case "_app_data_root": VerifyCount(functionCall, 0); TranslateAppDataRoot(output); break; case "_array_get": VerifyCount(functionCall, 2); TranslateArrayGet(output, args[0], args[1]); break; case "_array_join": VerifyCount(functionCall, 2); TranslateArrayJoin(output, args[0], args[1]); break; case "_array_length": VerifyCount(functionCall, 1); TranslateArrayLength(output, args[0]); break; case "_array_set": VerifyCount(functionCall, 3); TranslateArraySet(output, args[0], args[1], args[2]); break; case "_assert": VerifyCount(functionCall, 1); TranslateAssert(output, args[0]); break; case "_byte_code_get_int_args": VerifyCount(functionCall, 0); TranslateByteCodeGetIntArgs(output); break; case "_byte_code_get_ops": VerifyCount(functionCall, 0); TranslateByteCodeGetOps(output); break; case "_byte_code_get_string_args": VerifyCount(functionCall, 0); TranslateByteCodeGetStringArgs(output); break; case "_byte_code_get_raw_string": VerifyCount(functionCall, 0); TranslateGetRawByteCodeString(output); break; case "_cast": VerifyCount(functionCall, 2); TranslateCast(output, (StringConstant)args[0], args[1]); break; case "_cast_to_list": VerifyCount(functionCall, 2); TranslateCastToList(output, (StringConstant)args[0], args[1]); break; case "_char_to_string": VerifyCount(functionCall, 1); TranslateCharToString(output, args[0]); break; case "_chr": VerifyCount(functionCall, 1); TranslateChr(output, args[0]); break; case "_comment": VerifyCount(functionCall, 1); TranslateComment(output, (StringConstant)args[0]); break; case "_convert_list_to_array": VerifyCount(functionCall, 2); TranslateConvertListToArray(output, (StringConstant)args[0], args[1]); break; case "_current_time_seconds": VerifyCount(functionCall, 0); TranslateCurrentTimeSeconds(output); break; case "_dictionary_contains": VerifyCount(functionCall, 2); TranslateDictionaryContains(output, args[0], args[1]); break; case "_dictionary_get_guaranteed": VerifyCount(functionCall, 2); TranslateDictionaryGetGuaranteed(output, args[0], args[1]); break; case "_dictionary_get_keys": VerifyCount(functionCall, 2); TranslateDictionaryGetKeys(output, ((StringConstant)args[0]).Value, args[1]); break; case "_dictionary_get_values": VerifyCount(functionCall, 1); TranslateDictionaryGetValues(output, args[0]); break; case "_dictionary_remove": VerifyCount(functionCall, 2); TranslateDictionaryRemove(output, args[0], args[1]); break; case "_dictionary_set": VerifyCount(functionCall, 3); TranslateDictionarySet(output, args[0], args[1], args[2]); break; case "_dictionary_size": VerifyCount(functionCall, 1); TranslateDictionarySize(output, args[0]); break; case "_dot_equals": VerifyCount(functionCall, 2); TranslateDotEquals(output, args[0], args[1]); break; case "_enqueue_vm_resume": VerifyCount(functionCall, 2); TranslateEnqueueVmResume(output, args[0], args[1]); break; case "_force_parens": VerifyCount(functionCall, 1); TranslateForceParens(output, args[0]); break; case "_get_program_data": VerifyCount(functionCall, 0); TranslateGetProgramData(output); break; case "_int": VerifyCount(functionCall, 1); TranslateInt(output, args[0]); break; case "_is_valid_integer": VerifyCount(functionCall, 1); TranslateIsValidInteger(output, args[0]); break; case "_list_clear": VerifyCount(functionCall, 1); TranslateListClear(output, args[0]); break; case "_list_concat": VerifyCount(functionCall, 2); TranslateListConcat(output, args[0], args[1]); break; case "_list_get": VerifyCount(functionCall, 2); TranslateListGet(output, args[0], args[1]); break; case "_list_insert": VerifyCount(functionCall, 3); TranslateListInsert(output, args[0], args[1], args[2]); break; case "_list_join": VerifyCount(functionCall, 2); TranslateListJoin(output, args[0], args[1]); break; case "_list_join_chars": VerifyCount(functionCall, 1); TranslateListJoinChars(output, args[0]); break; case "_list_last_index": VerifyCount(functionCall, 1); TranslateListLastIndex(output, args[0]); break; case "_list_length": VerifyCount(functionCall, 1); TranslateListLength(output, args[0]); break; case "_list_pop": VerifyCount(functionCall, 1); TranslateListPop(output, args[0]); break; case "_list_push": VerifyCount(functionCall, 2); TranslateListPush(output, args[0], args[1]); break; case "_list_remove_at": VerifyCount(functionCall, 2); TranslateListRemoveAt(output, args[0], args[1]); break; case "_list_reverse_in_place": VerifyCount(functionCall, 1); TranslateListReverseInPlace(output, args[0]); break; case "_list_set": VerifyCount(functionCall, 3); TranslateListSet(output, args[0], args[1], args[2]); break; case "_list_shuffle_in_place": VerifyCount(functionCall, 1); TranslateListShuffleInPlace(output, args[0]); break; case "_math_arc_cos": VerifyCount(functionCall, 1); TranslateArcCos(output, args[0]); break; case "_math_arc_sin": VerifyCount(functionCall, 1); TranslateArcSin(output, args[0]); break; case "_math_arc_tan": VerifyCount(functionCall, 2); TranslateArcTan(output, args[0], args[1]); break; case "_math_cos": VerifyCount(functionCall, 1); TranslateCos(output, args[0]); break; case "_math_log": VerifyCount(functionCall, 1); TranslateMathLog(output, args[0]); break; case "_math_pow": VerifyCount(functionCall, 2); TranslateExponent(output, args[0], args[1]); break; case "_math_sin": VerifyCount(functionCall, 1); TranslateSin(output, args[0]); break; case "_math_tan": VerifyCount(functionCall, 1); TranslateTan(output, args[0]); break; case "_multiply_list": VerifyCount(functionCall, 2); TranslateMultiplyList(output, args[0], args[1]); break; case "_new_array": VerifyCount(functionCall, 2); TranslateNewArray(output, (StringConstant)args[0], args[1]); break; case "_new_dictionary": VerifyCount(functionCall, 2); TranslateNewDictionary(output, (StringConstant)args[0], (StringConstant)args[1]); break; case "_new_list": VerifyCount(functionCall, 1); TranslateNewList(output, (StringConstant)args[0]); break; case "_new_list_of_size": VerifyCount(functionCall, 2); TranslateNewListOfSize(output, (StringConstant)args[0], args[1]); break; case "_ord": VerifyCount(functionCall, 1); TranslateOrd(output, args[0]); break; case "_parse_float": VerifyCount(functionCall, 2); TranslateParseFloat(output, args[0], args[1]); break; case "_parse_int": VerifyCount(functionCall, 1); TranslateParseInt(output, args[0]); break; case "_postfix_decrement": VerifyCount(functionCall, 1); TranslateIncrement(output, args[0], false, false); break; case "_postfix_increment": VerifyCount(functionCall, 1); TranslateIncrement(output, args[0], true, false); break; case "_prefix_decrement": VerifyCount(functionCall, 1); TranslateIncrement(output, args[0], false, true); break; case "_prefix_increment": VerifyCount(functionCall, 1); TranslateIncrement(output, args[0], true, true); break; case "_print_stderr": VerifyCount(functionCall, 1); TranslatePrint(output, args[0], true); break; case "_print_stdout": VerifyCount(functionCall, 1); TranslatePrint(output, args[0], false); break; case "_random_float": VerifyCount(functionCall, 0); TranslateRandomFloat(output); break; case "_resource_get_manifest": VerifyCount(functionCall, 0); TranslateResourceGetManifest(output); break; case "_resource_read_text_file": VerifyCount(functionCall, 1); TranslateResourceReadText(output, args[0]); break; case "_set_program_data": VerifyCount(functionCall, 1); TranslateSetProgramData(output, args[0]); break; case "_sorted_copy_of_int_array": VerifyCount(functionCall, 1); TranslateSortedCopyOfIntArray(output, args[0]); break; case "_sorted_copy_of_string_array": VerifyCount(functionCall, 1); TranslateSortedCopyOfStringArray(output, args[0]); break; case "_string_append": VerifyCount(functionCall, 2); TranslateStringAppend(output, args[0], args[1]); break; case "_string_as_char": VerifyCount(functionCall, 1); TranslateStringAsChar(output, (StringConstant)args[0]); break; case "_string_cast_strong": VerifyCount(functionCall, 1); TranslateStringCast(output, args[0], true); break; case "_string_cast_weak": VerifyCount(functionCall, 1); TranslateStringCast(output, args[0], false); break; case "_string_char_at": VerifyCount(functionCall, 2); TranslateStringCharAt(output, args[0], args[1]); break; case "_string_char_code_at": VerifyCount(functionCall, 2); TranslateStringCharCodeAt(output, args[0], args[1]); break; case "_string_compare": VerifyCount(functionCall, 2); TranslateStringCompare(output, args[0], args[1]); break; case "_string_concat": VerifyCountAtLeast(functionCall, 2); TranslateStringConcat(output, args); break; case "_string_contains": VerifyCount(functionCall, 2); TranslateStringContains(output, args[0], args[1]); break; case "_string_endswith": VerifyCount(functionCall, 2); TranslateStringEndsWith(output, args[0], args[1]); break; case "_string_equals": VerifyCount(functionCall, 2); TranslateStringEquals(output, args[0], args[1]); break; case "_string_from_code": VerifyCount(functionCall, 1); TranslateStringFromCode(output, args[0]); break; case "_string_index_of": VerifyCount(functionCall, 2, 3); TranslateStringIndexOf(output, args[0], args[1], args.Length == 3 ? args[2] : null); break; case "_string_length": VerifyCount(functionCall, 1); TranslateStringLength(output, args[0]); break; case "_string_lower": VerifyCount(functionCall, 1); TranslateStringLower(output, args[0]); break; case "_string_parse_float": VerifyCount(functionCall, 1); TranslateStringParseFloat(output, args[0]); break; case "_string_parse_int": VerifyCount(functionCall, 1); TranslateStringParseInt(output, args[0]); break; case "_string_reverse": VerifyCount(functionCall, 1); TranslateStringReverse(output, args[0]); break; case "_string_replace": VerifyCount(functionCall, 3); TranslateStringReplace(output, args[0], args[1], args[2]); break; case "_string_split": VerifyCount(functionCall, 2); TranslateStringSplit(output, args[0], args[1]); break; case "_string_startswith": VerifyCount(functionCall, 2); TranslateStringStartsWith(output, args[0], args[1]); break; case "_string_substring": VerifyCount(functionCall, 2, 3); TranslateStringSubstring(output, args[0], args[1], args.Length > 2 ? args[2] : null); break; case "_string_substring_exists_at": VerifyCount(functionCall, 3); TranslateStringSubstringExistsAt(output, args[0], args[1], args[2]); break; case "_string_trim": VerifyCount(functionCall, 1); TranslateStringTrim(output, args[0]); break; case "_string_upper": VerifyCount(functionCall, 1); TranslateStringUpper(output, args[0]); break; case "_thread_sleep": VerifyCount(functionCall, 1); TranslateThreadSleep(output, args[0]); break; case "_unsafe_float_division": VerifyCount(functionCall, 2); TranslateUnsafeFloatDivision(output, args[0], args[1]); break; case "_unsafe_integer_division": VerifyCount(functionCall, 2); TranslateUnsafeIntegerDivision(output, args[0], args[1]); break; default: throw new ParserException(functionCall.FirstToken, "Unrecognized system method invocation: " + functionCall.Name); } }