diff --git a/editors/vscode/src/extension.ts b/editors/vscode/src/extension.ts index 7e09fc73..2f9a3635 100644 --- a/editors/vscode/src/extension.ts +++ b/editors/vscode/src/extension.ts @@ -12,7 +12,7 @@ let client: LanguageClient; export async function activate(context: ExtensionContext) { const serverOptions: ServerOptions = { run: { - command: "elk --lsp", + command: "elk-lsp", transport: TransportKind.stdio, }, debug: { diff --git a/examples/parser/lexer.elk b/examples/parser/lexer.elk new file mode 100644 index 00000000..4e11ddb4 --- /dev/null +++ b/examples/parser/lexer.elk @@ -0,0 +1,3 @@ +pub fn lex(input) { + input | split " " +} \ No newline at end of file diff --git a/examples/parser/main.elk b/examples/parser/main.elk new file mode 100644 index 00000000..7d67ccd6 --- /dev/null +++ b/examples/parser/main.elk @@ -0,0 +1,6 @@ +with ./parser + +fn main() { + parser::parse("") + | lexer::lex() +} \ No newline at end of file diff --git a/examples/parser/parser.elk b/examples/parser/parser.elk new file mode 100644 index 00000000..9fdc2e84 --- /dev/null +++ b/examples/parser/parser.elk @@ -0,0 +1,6 @@ +with ./lexer + +pub fn parse(input) { + let tokens = lexer::lex(input) + tokens +} \ No newline at end of file diff --git a/src/ElkProgram.cs b/src/ElkProgram.cs index 518b0e7d..99c53250 100644 --- a/src/ElkProgram.cs +++ b/src/ElkProgram.cs @@ -112,6 +112,7 @@ out var error { SemanticTokens = semanticTokens, Diagnostics = diagnostics, + Ast = ast, }; } } @@ -125,9 +126,13 @@ internal static EvaluationResult Evaluate( Debug.Assert(scope.ModuleScope is not { Ast: null }); var generated = GeneratePages(ast, scope, analysisScope, virtualMachine).ToList(); - var result = generated - .Select(pair => virtualMachine?.Execute(pair.page)) - .LastOrDefault(); + RuntimeObject? result = null; + if (virtualMachine != null) + { + result = generated + .Select(pair => virtualMachine.Execute(pair.page!)) + .LastOrDefault(); + } return new EvaluationResult { @@ -136,7 +141,7 @@ internal static EvaluationResult Evaluate( }; } - private static IEnumerable<(Ast analysedAst, Page page)> GeneratePages( + private static IEnumerable<(Ast analysedAst, Page? page)> GeneratePages( Ast ast, Scope scope, AnalysisScope analysisScope, @@ -154,16 +159,12 @@ internal static EvaluationResult Evaluate( .Concat(EvaluateModules(scope.ModuleScope.Modules, virtualMachine)); var analysedAst = Analyser.Analyse(ast, scope.ModuleScope, analysisScope); - if (virtualMachine != null) - { - var result = virtualMachine.Generate(analysedAst); - pages = pages.Append((analysedAst, result)); - } + var result = virtualMachine?.Generate(analysedAst); - return pages; + return pages.Append((analysedAst, result)); } - private static IEnumerable<(Ast, Page)> EvaluateModules( + private static IEnumerable<(Ast, Page?)> EvaluateModules( IEnumerable modules, VirtualMachine? virtualMachine) { diff --git a/src/Parsing/AstNodes.cs b/src/Parsing/AstNodes.cs index 4dd28fdb..7d4f6fd2 100644 --- a/src/Parsing/AstNodes.cs +++ b/src/Parsing/AstNodes.cs @@ -457,7 +457,7 @@ public class CallExpr( Scope scope, TextPos endPos) : Expr( - modulePath.FirstOrDefault()?.Position ?? identifier.Position, + GetStartPosition(modulePath, identifier, arguments), endPos, scope ) @@ -491,7 +491,19 @@ public class CallExpr( public FunctionExpr? EnclosingClosureProvidingFunction { get; init; } public override IEnumerable ChildExpressions - => Arguments; + => PipedToProgram == null + ? Arguments + : Arguments.Prepend(PipedToProgram); + + private static TextPos GetStartPosition(IList modulePath, Token identifier, IList arguments) + { + var callPos = modulePath.FirstOrDefault()?.Position ?? identifier.Position; + var firstArgumentPosition = arguments.FirstOrDefault()?.StartPosition; + + return firstArgumentPosition?.Index < callPos.Index + ? firstArgumentPosition + : callPos; + } } public class LiteralExpr(Token value, Scope scope)