Added comments

This commit is contained in:
2024-12-06 22:05:41 +01:00
parent d2cbdc2910
commit b3f75ae3d8
15 changed files with 128 additions and 12 deletions

View File

@ -29,7 +29,7 @@ namespace fsh {
class AstFactory {
public:
// Generates an abstract syntax tree
// Generise abstraktno sintaksno drvo po ebnf formuli gore
static std::shared_ptr<ExecutableNode> generate_ast(std::list<Token>& list) {
auto it = list.begin();
return CommandLineNode::build(it);
@ -41,4 +41,4 @@ namespace fsh {
AstFactory(const AstFactory&) = default;
};
} // namespace fsh
}

View File

@ -33,6 +33,7 @@ namespace fsh {
using TokenType = Lexer::TokenType;
AstNode(NodeType type, std::string name) : type(type), name(name) {}
// Pokusa da napravi od narednih tokena zadati Node ako ne uspe vrati null
template <class T>
static std::shared_ptr<T> Optional(std::list<Token>::iterator& it, std::shared_ptr<T>& node_ptr);
@ -42,6 +43,7 @@ namespace fsh {
return Optional<T>(it, node_ptr);
}
// Pravi od narednih tokena zadati Node ako ne uspe izbaci AstBuildError
template <class T>
static std::shared_ptr<T> Mandatory(std::list<Token>::iterator& it) {
return T::build(it);
@ -70,4 +72,4 @@ namespace fsh {
AstBuildError(std::string err) : std::runtime_error(err) {}
};
} // namespace fsh
}

View File

@ -80,4 +80,4 @@ namespace fsh {
bool append = false;
};
} // namespace fsh
}

View File

@ -66,4 +66,4 @@ namespace fsh {
std::shared_ptr<ExecutableNode> pipe;
};
} // namespace fsh
}

View File

@ -6,6 +6,7 @@
namespace fsh {
// Genericni node namenjen kao wrapper za tokene
template <Lexer::TokenType T>
class TokenNode : public AstNode {
public:

View File

@ -34,21 +34,25 @@ namespace fsh {
bool extends; //Not implemented
};
// Dodavanje argumenta koji sluzi za ulaz
void add_input_rule() {
has_input = true;
pos_arg_rules.push_back({ &_Argument::create<ArgInput>, false, false });
}
// Dodavanje pozicionog argumenta sa automatskim prebacivanjem
template <typename T>
void add_rule(bool mandatory, bool extends = false) {
pos_arg_rules.push_back({ &_Argument::create<Argument<T> >, mandatory, extends });
}
// Dodavanje pravila za flag (sa mogucnoscu da se pokupi argument priljepljen npr. -n<count>)
template <typename T = bool>
void add_rule(const std::string name, bool capturing = false) {
flag_rules[name] = { _Argument::create<Argument<T> >, false, capturing, false };
}
// Popunjava ArgManager (Mozda bolji naziv bi bio populate?)
void parse(ArgManager& manager, ArgNodes& vec, FlagNode flag);
bool ghas_input() { return has_input; }

View File

@ -12,6 +12,7 @@
namespace fsh {
// Ovaj singleton odrzava podatke o komanadama
class CommandRegistry {
public:
static CommandRegistry& instance() {
@ -27,6 +28,7 @@ namespace fsh {
Command& operator[](const std::string n) { return get(n); }
private:
// Nove komande bi registrovali ovde:
CommandRegistry() {
cmds["wc"] = Command::register_cmd<CmdWc>();
cmds["date"] = Command::register_cmd<CmdDate>();
@ -40,4 +42,4 @@ namespace fsh {
std::unordered_map<std::string, std::unique_ptr<Command> > cmds;
};
} // namespace fsh
}

View File

@ -27,7 +27,10 @@ namespace fsh {
}
protected:
// Register_flags se zove jednom pri registraciji komande i sluzi da u ArgFactory postavis
// pravila za argumente i opcije
virtual void register_flags() {};
// Korisnik u ovoj funkciju implementira glavnu funkcionalnost komande
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) {};
ArgFactory& get_factory() { return arg_factory; }

View File

@ -8,9 +8,11 @@
namespace fsh {
//Glavna singleton klasa za shell
class fsh {
public:
std::unordered_map<std::string, std::string> environment;
std::unordered_map<std::string, std::string>
environment; //Omogucava komandama da cuvaju informacije kroz upotrebe
static fsh& instance() {
static fsh f;

View File

@ -6,13 +6,13 @@
namespace fsh {
// Ova klasa vrsi leksicku analizu ulaznog string-a
class Lexer {
public:
enum TokenType {
WHITESPACE,
WORD,
STRING_LITERAL,
OPT,
FLAG,
LREDIRECTION,
RREDIRECTION,
@ -23,8 +23,11 @@ namespace fsh {
using Token = std::pair<Lexer::TokenType, std::string>;
int peek();
int consume();
// Daje nam sledeci karakter bez povecavanjem brojaca
int peek();
// Daje nam sledeci karakter sa povecavanjem brojaca
int consume();
// Trazi za sledeci token
Token next_token();
static std::list<Token> process(std::string line);
@ -87,4 +90,4 @@ namespace fsh {
return state;
}
} // namespace fsh
}

View File

@ -7,6 +7,7 @@
#include <iostream>
#endif
// Neke "workarounds" za terminale na Unix i Windows
namespace fsh::util {
/**

View File

@ -5,6 +5,7 @@
#include <fstream>
#include <stdexcept>
// Razlicite pomocne funkcije za tekstove
namespace fsh::util {
extern const char* tokens[];

View File

@ -26,6 +26,8 @@ namespace fsh {
manager.push_arg(build_arg(pos_arg_rules[i].build, arg));
if (!pos_arg_rules[i].extends) i++;
}
if (pos_arg_rules.size() > i && pos_arg_rules[i].mandatory)
throw std::invalid_argument("Excpected More arguments");
}
void ArgFactory::parse_flag(ArgManager& manager, FlagNode flag) {

View File

@ -19,4 +19,4 @@ namespace fsh {
in.clear();
}
} // namespace fsh
}

95
util.hpp Normal file
View File

@ -0,0 +1,95 @@
/** CmakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(fsh LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(FSH_SOURCE_FILES
src/main.cpp
src/fsh.cpp
src/lexer.cpp
src/ast/ast.cpp
src/ast/ast_exec.cpp
src/ast/ast_print.cpp
src/util/input.cpp
src/util/text.cpp
src/cmd/arg.cpp
src/cmd/cmd_base.cpp
)
include_directories(include)
add_executable(fsh ${FSH_SOURCE_FILES})
*/
/** .clang-format
---
Language: Cpp
BasedOnStyle: Mozilla
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: true
AlignConsecutiveAssignments: true
AlignEscapedNewlines: Right
AlignOperands: false
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: false
BreakConstructorInitializers: AfterColon
ColumnLimit: 120
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
IncludeBlocks: Preserve
IndentCaseLabels: true
IndentWidth: 4
PointerAlignment: Left
ReflowComments: false
SortIncludes: false
SortUsingDeclarations: false
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 4
UseTab: Never
AllowShortEnumsOnASingleLine: false
BraceWrapping:
AfterEnum: false
AlignConsecutiveDeclarations:
Enabled: true
NamespaceIndentation: All
*/