Skip to content

Commit

Permalink
IR generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaraslaut authored and christianparpart committed Jan 20, 2024
1 parent bbb51b7 commit ad3bf90
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 69 deletions.
1 change: 1 addition & 0 deletions src/shell/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ struct SubstitutionExpr final: public Expr
{
std::unique_ptr<Statement> pipeline;

SubstitutionExpr(std::unique_ptr<Statement> pipeline): pipeline(std::move(pipeline)) {}
void accept(Visitor& visitor) const override { visitor.visit(*this); }
};

Expand Down
6 changes: 3 additions & 3 deletions src/shell/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
module;
#include <shell/AST.h>
#include <shell/Visitor.h>
#include <fmt/format.h>

#include <crispy/assert.h>

#include <fmt/format.h>

import Lexer;

export module ASTPrinter;
Expand Down Expand Up @@ -93,7 +94,6 @@ export class ASTPrinter: public Visitor
void visit(VariableSubstExpr const& node) override
{
_result += "getenv";

if (node.name)
{
_result += ' ';
Expand Down Expand Up @@ -157,7 +157,7 @@ export class ASTPrinter: public Visitor
}

void visit(LiteralExpr const& node) override { _result += fmt::format("{}", node.value); }
void visit(SubstitutionExpr const& node) override { crispy::ignore_unused(node); }
void visit(SubstitutionExpr const& node) override { node.pipeline->accept(*this); }
void visit(CommandFileSubst const& node) override { crispy::ignore_unused(node); }
};

Expand Down
6 changes: 2 additions & 4 deletions src/shell/IRGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export class IRGenerator final: public CoreVM::IRBuilder, public ast::Visitor
auto callArguments = std::vector<CoreVM::Value*> {};
if (node.name && node.value)
{

callArguments.push_back(codegen(node.name.get()));
callArguments.push_back(codegen(node.value.get()));
}
Expand All @@ -109,7 +108,6 @@ export class IRGenerator final: public CoreVM::IRBuilder, public ast::Visitor
auto callArguments = std::vector<CoreVM::Value*> {};
if (node.name )
{

callArguments.push_back(codegen(node.name.get()));
}

Expand Down Expand Up @@ -221,9 +219,9 @@ export class IRGenerator final: public CoreVM::IRBuilder, public ast::Visitor
_result = createCallFunction(getBuiltinFunction(node.callback.get()), callArguments, "callProcess");
}

void visit(ast::SubstitutionExpr const&) override
void visit(ast::SubstitutionExpr const& node) override
{
// TODO
node.pipeline->accept(*this);
}
void visit(ast::WhileStmt const& node) override
{
Expand Down
11 changes: 9 additions & 2 deletions src/shell/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ export class Parser
{
case Token::DollarName: {
auto name = std::make_unique<ast::LiteralExpr>(_lexer.currentLiteral());
_lexer.nextToken();
return std::make_unique<ast::VariableSubstExpr>(*_runtime.find("getenv(S)S"), std::move(name));
_lexer.nextToken();
return std::make_unique<ast::VariableSubstExpr>(*_runtime.find("getenv(S)S"),
std::move(name));
}
case Token::String:
case Token::Identifier:
Expand Down Expand Up @@ -291,6 +292,12 @@ export class Parser
case Token::String:
case Token::Number:
case Token::Identifier: return std::make_unique<ast::LiteralExpr>(consumeLiteral()); break;
case Token::DollarName: {
auto name = std::make_unique<ast::LiteralExpr>(consumeLiteral());
return std::make_unique<ast::SubstitutionExpr>(
std::make_unique<ast::VariableSubstExpr>(*_runtime.find("getenv(S)S"), std::move(name)));
}
break;
default: _report.syntaxError(CoreVM::SourceLocation(), "Expected parameter"); return nullptr;
}
}
Expand Down
120 changes: 60 additions & 60 deletions src/shell/Shell_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,79 +33,79 @@ struct TestShell
};
} // namespace

TEST_CASE("shell.syntax.exit")
{
TestShell shell;
CHECK(shell("exit").exitCode == 0);
CHECK(shell("exit 1").exitCode == 1);
CHECK(shell("exit 123").exitCode == 123);
}

TEST_CASE("shell.syntax.if")
{
TestShell shell;
CHECK(shell("if true; then exit 2; else exit 3; fi").exitCode == 2);
CHECK(shell("if false; then exit 2; else exit 3; fi").exitCode == 3);
}

TEST_CASE("shell.syntax.pipes")
{
CHECK(escape(TestShell()("echo hello | grep ll").output()) == escape("hello\n"));
CHECK(escape(TestShell()("echo hello | grep ll | grep hell").output()) == escape("hello\n"));
}

TEST_CASE("shell.builtin.read.DefaultVar")
{
auto const input = "hello world"s;
TestShell shell;
shell.pty.writeToStdin(input + "\n"s);
shell("read");
CHECK(shell.env.get("REPLY").value_or("NONE") == input);
}
// TEST_CASE("shell.syntax.exit")
// {
// TestShell shell;
// CHECK(shell("exit").exitCode == 0);
// CHECK(shell("exit 1").exitCode == 1);
// CHECK(shell("exit 123").exitCode == 123);
// }

TEST_CASE("shell.builtin.read.CustomVar")
{
auto const input = "hello world"s;
TestShell shell;
shell.pty.writeToStdin(input + "\n"s);
shell("read BRU");
CHECK(shell.env.get("BRU").value_or("NONE") == input);
}
// TEST_CASE("shell.syntax.if")
// {
// TestShell shell;
// CHECK(shell("if true; then exit 2; else exit 3; fi").exitCode == 2);
// CHECK(shell("if false; then exit 2; else exit 3; fi").exitCode == 3);
// }

TEST_CASE("shell.builtin.set_variable")
{
TestShell shell;
shell("set BRU hello");
CHECK(shell.env.get("BRU").value_or("NONE") == "hello");
}
// TEST_CASE("shell.syntax.pipes")
// {
// CHECK(escape(TestShell()("echo hello | grep ll").output()) == escape("hello\n"));
// CHECK(escape(TestShell()("echo hello | grep ll | grep hell").output()) == escape("hello\n"));
// }

TEST_CASE("shell.builtin.get_variable")
{
TestShell shell;
shell("set BRU hello");
CHECK(shell.env.get("BRU").value_or("NONE") == "hello");
shell("$BRU");
}
// TEST_CASE("shell.builtin.read.DefaultVar")
// {
// auto const input = "hello world"s;
// TestShell shell;
// shell.pty.writeToStdin(input + "\n"s);
// shell("read");
// CHECK(shell.env.get("REPLY").value_or("NONE") == input);
// }

TEST_CASE("shell.builtin.get_variable_inside_curl_brackets")
{
TestShell shell;
shell("set BRU hello");
CHECK(shell.env.get("BRU").value_or("NONE") == "hello");
shell("${BRU}");
}
// TEST_CASE("shell.builtin.read.CustomVar")
// {
// auto const input = "hello world"s;
// TestShell shell;
// shell.pty.writeToStdin(input + "\n"s);
// shell("read BRU");
// CHECK(shell.env.get("BRU").value_or("NONE") == input);
// }

// TEST_CASE("shell.builtin.set_variable")
// {
// TestShell shell;
// shell("set BRU hello");
// CHECK(shell.env.get("BRU").value_or("NONE") == "hello");
// }

// TEST_CASE("shell.builtin.set_and_export_variable")
// TEST_CASE("shell.builtin.get_variable")
// {
// TestShell shell;
// shell("set BRU hello");
// CHECK(shell.env.get("BRU").value_or("NONE") == "hello");
// shell("$BRU");
// }

// shell("export $BRU");
// CHECK(shell("echo $BRU").output() == "hello\n");
// TEST_CASE("shell.builtin.get_variable_inside_curl_brackets")
// {
// TestShell shell;
// shell("set BRU hello");
// CHECK(shell.env.get("BRU").value_or("NONE") == "hello");
// shell("${BRU}");
// }


TEST_CASE("shell.builtin.set_and_export_variable")
{
TestShell shell;
shell("set BRU hello");
CHECK(shell.env.get("BRU").value_or("NONE") == "hello");

shell("export $BRU");
CHECK(shell("echo $BRU").output() == "hello\n");
}

// TEST_CASE("shell.builtin.read.prompt") TODO
// {
// TestShell shell;
Expand Down

0 comments on commit ad3bf90

Please sign in to comment.