Skip to content

Commit

Permalink
First 'alfa' release
Browse files Browse the repository at this point in the history
  • Loading branch information
Irwin1985 committed Mar 4, 2023
1 parent f4a6db9 commit da9e2e3
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 43 deletions.
Binary file added Hungaro.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion src/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -2613,12 +2613,13 @@ public Object call(Interpreter interpreter, List<Object> arguments) {
}
final String connectionString = engine.getConnectionString(server, port, database);
try {
Class.forName(engine.getDriverClass());
Connection connection = DriverManager.getConnection(connectionString, user, password);
Environment connectionEnv = interpreter.makeObject(connection, interpreter.connectionEnv, "Connection");
// add the driver name to the connection environment
connectionEnv.define("driver", driver);
return connectionEnv;
} catch (SQLException e) {
} catch (SQLException | ClassNotFoundException e) {
throw new RuntimeError(null, "Could not connect to the database: " + e.getMessage());
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/EngineManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

public abstract class EngineManager {
public abstract String getConnectionString(String server, Object port, String database);
public abstract String getDriverClass();
public abstract String getTables(String database);
public abstract String getColumns(String database, String tableName);
public abstract String getPrimaryKey(String database, String tableName);
Expand All @@ -18,6 +19,11 @@ public String getConnectionString(String server, Object port, String database) {
return String.format("jdbc:mysql://%s:%s/%s?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", server, port, database);
}

@Override
public String getDriverClass() {
return "com.mysql.cj.jdbc.Driver";
}

@Override
public String getTables(String database) {
return "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '" + database + "'";
Expand Down Expand Up @@ -65,6 +71,11 @@ public String getConnectionString(String server, Object port, String database) {
return url;
}

@Override
public String getDriverClass() {
return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
}

@Override
public String getTables(String database) {
return "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = '" + database + "'";
Expand Down
55 changes: 44 additions & 11 deletions src/Hungaro.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Hungaro {
private static final Interpreter interpreter = new Interpreter();
static boolean hadError = false;
static boolean hadRuntimeError = false;
static boolean debugMode = true;
static boolean debugMode = false; // debug mode

// foreground colors
public static final String ANSI_RESET = "\u001B[0m";
Expand Down Expand Up @@ -98,19 +98,52 @@ public static void runFile(String path) throws IOException {
public static void runPrompt() throws IOException {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);

printPrompt();
for (;;) {
System.out.print(">>> ");
System.out.print(">>> ");
String line = reader.readLine();
if (line == null) break;

// Start REPL commands
if (line.equals("exit")) break;
if (line.equals("help")) {
printHelp();
continue;
}
if (line.startsWith("run ")) {
final String file = line.substring(4).trim();
runFile(file);
continue;
}
if (line.equals("cls")) {
System.out.print("\033[H\033[2J");
System.out.flush();
printPrompt();
continue;
}
// End REPL commands

// run line if not a command
run(line);
hadError = false; // ignore errors in REPL mode.
}
}

private static void printPrompt() {
System.out.println("Hungaro v0.1.0");
System.out.println("Date: " + new java.util.Date());
printHelp();
}

private static void printHelp() {
System.out.println("Type 'help' for help.");
System.out.println("Type 'exit' to exit.");
System.out.println("Type 'run <file>' to run a file.");
}

public static void run(String source) {
List<Token> tokens = null;
// try {
try {
final Scanner scanner = new Scanner(source);
tokens = scanner.scanTokens();
final boolean printTokens = false;
Expand All @@ -123,20 +156,20 @@ public static void run(String source) {
System.out.println("=============================");
}
// end debug
// } catch (Exception e) {
// System.err.println(e.getMessage());
// }
} catch (Exception e) {
System.err.println(e.getMessage());
}
if (hadError) return;

// continue parsing
Parser parser = new Parser(tokens);
List<Stmt> statements = parser.parse();
if (hadError) return;
// try {
try {
interpreter.interpret(statements);
// } catch(Exception e) {
// System.err.println(e.getMessage());
// }
} catch(Exception e) {
System.err.println(e.getMessage());
}
}

static void error(int line, int col, String message) {
Expand Down
26 changes: 13 additions & 13 deletions src/Scanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import java.util.regex.Pattern;

// DEBUG
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
// import java.io.IOException;
// import java.nio.charset.Charset;
// import java.nio.file.Files;
// import java.nio.file.Paths;
// DEBUG

public class Scanner {
Expand Down Expand Up @@ -318,15 +318,15 @@ private Token getNextToken() {
return null;
}

public static void main(String[] args) throws IOException {
byte[] bytes = Files.readAllBytes(Paths.get("F:\\Desarrollo\\GitHub\\Hungaro\\sample.hgr"));
String source = new String(bytes, Charset.defaultCharset());
// public static void main(String[] args) throws IOException {
// byte[] bytes = Files.readAllBytes(Paths.get("F:\\Desarrollo\\GitHub\\Hungaro\\sample.hgr"));
// String source = new String(bytes, Charset.defaultCharset());

Scanner sc = new Scanner(source);
List<Token> tokens = sc.scanTokens();
// Scanner sc = new Scanner(source);
// List<Token> tokens = sc.scanTokens();

for (Token tok : tokens) {
System.out.println(tok);
}
}
// for (Token tok : tokens) {
// System.out.println(tok);
// }
// }
}
18 changes: 0 additions & 18 deletions test.java

This file was deleted.

0 comments on commit da9e2e3

Please sign in to comment.