Skip to content

Commit

Permalink
lsp: Fix AST not being returned and piped values not being included i…
Browse files Browse the repository at this point in the history
…n the call start position
  • Loading branch information
PaddiM8 committed Aug 25, 2024
1 parent 9245e5b commit 9ea2777
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion editors/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
3 changes: 3 additions & 0 deletions examples/parser/lexer.elk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn lex(input) {
input | split " "
}
6 changes: 6 additions & 0 deletions examples/parser/main.elk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
with ./parser

fn main() {
parser::parse("")
| lexer::lex()
}
6 changes: 6 additions & 0 deletions examples/parser/parser.elk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
with ./lexer

pub fn parse(input) {
let tokens = lexer::lex(input)
tokens
}
23 changes: 12 additions & 11 deletions src/ElkProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ out var error
{
SemanticTokens = semanticTokens,
Diagnostics = diagnostics,
Ast = ast,
};
}
}
Expand All @@ -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
{
Expand All @@ -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,
Expand All @@ -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<ModuleScope> modules,
VirtualMachine? virtualMachine)
{
Expand Down
16 changes: 14 additions & 2 deletions src/Parsing/AstNodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ public class CallExpr(
Scope scope,
TextPos endPos)
: Expr(
modulePath.FirstOrDefault()?.Position ?? identifier.Position,
GetStartPosition(modulePath, identifier, arguments),
endPos,
scope
)
Expand Down Expand Up @@ -491,7 +491,19 @@ public class CallExpr(
public FunctionExpr? EnclosingClosureProvidingFunction { get; init; }

public override IEnumerable<Expr> ChildExpressions
=> Arguments;
=> PipedToProgram == null
? Arguments
: Arguments.Prepend(PipedToProgram);

private static TextPos GetStartPosition(IList<Token> modulePath, Token identifier, IList<Expr> 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)
Expand Down

0 comments on commit 9ea2777

Please sign in to comment.