Simple Inlining Engine in Go
This challenge asks you to implement a basic inlining engine for a simplified expression language in Go. Inlining is a compiler optimization technique where function calls are replaced with the body of the called function, reducing overhead and potentially enabling further optimizations. This exercise will help you understand the core concepts of inlining and its impact on code generation.
Problem Description
You are tasked with creating a function Inline(expression string, functionName string, functionBody string) string. This function takes three string arguments:
expression: The expression string containing a call to the function to be inlined. The expression will be a simple string representing a function call in the formatfunctionName(arg1, arg2, ...). Arguments are comma-separated.functionName: The name of the function to be inlined.functionBody: The body of the function to be inlined. This will be a string containing the function's code.
The function should replace the function call in the expression with the functionBody. All occurrences of functionName within the functionBody should be replaced with the arguments from the original function call. The arguments should be inserted in the order they appear in the function call.
Key Requirements:
- String Manipulation: You'll need to perform string manipulation to identify the function call, extract arguments, and replace the call with the function body.
- Argument Replacement: The core of the challenge is correctly replacing the function name with the arguments.
- Error Handling: While not strictly required for a simple inliner, consider how you might handle cases where the function call is malformed (e.g., missing parentheses, invalid arguments). For this challenge, you can assume the input is well-formed.
- No External Libraries: You should solve this using only standard Go libraries.
Expected Behavior:
The function should return the modified expression string with the function call inlined.
Edge Cases to Consider:
- Function names appearing within the function body that are not the function being inlined.
- Empty argument lists.
- Nested function calls (this challenge does not require handling nested calls).
Examples
Example 1:
Input: expression = "add(2, 3)", functionName = "add", functionBody = "x + y"
Output: "2 + 3"
Explanation: The function call "add(2, 3)" is replaced with "2 + 3" because the 'add' function body is "x + y" and 'add' is replaced with '2', and 'x' is replaced with '2' and 'y' is replaced with '3'.
Example 2:
Input: expression = "multiply(4, 5, 6)", functionName = "multiply", functionBody = "a * b * c"
Output: "4 * 5 * 6"
Explanation: The function call "multiply(4, 5, 6)" is replaced with "4 * 5 * 6" because the 'multiply' function body is "a * b * c" and 'multiply' is replaced with '4', 'a' with '4', 'b' with '5', and 'c' with '6'.
Example 3:
Input: expression = "greet()", functionName = "greet", functionBody = "print(\"Hello, world!\")"
Output: "print(\"Hello, world!\")"
Explanation: The function call "greet()" is replaced with "print(\"Hello, world!\")" because the 'greet' function body is "print(\"Hello, world!\")" and 'greet' is replaced with nothing (empty string).
Constraints
expressionlength: Maximum 256 characters.functionNamelength: Maximum 32 characters.functionBodylength: Maximum 1024 characters.- The function call will always be in the format
functionName(arg1, arg2, ...) - Arguments will be simple strings without nested function calls or complex expressions.
- The function body will not contain the function name being inlined.
Notes
- Focus on the core logic of replacing the function call with the function body and substituting arguments.
- Consider using
strings.ReplaceAllfor simple replacements. - Think about how to split the arguments from the function call string.
strings.Splitcan be helpful. - This is a simplified inlining engine. Real-world inlining engines are much more complex and consider factors like code size, performance, and register allocation.
- Error handling is not a primary focus for this challenge, but consider how you might add it in a more robust implementation.