Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semantico finalizado #47

Merged
merged 30 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
63ec78c
Iniciado o desenvolvimento do Semantico e Tabela de simbolos
iaglourenco Oct 6, 2022
63ee9ea
Tabela de simbolos, semantico criados parcialmente
iaglourenco Oct 7, 2022
b2904a1
analisa leitura
iaglourenco Sep 21, 2022
9158785
ajustes no CSS
iaglourenco Sep 21, 2022
6900f75
Ajustes nos erros e correção de bugs
iaglourenco Sep 22, 2022
13e4bd2
Fix analisaDeclVar
iaglourenco Sep 22, 2022
fc0e2c3
Atualizado documentação
iaglourenco Sep 22, 2022
93111a5
Correções de erros
iaglourenco Sep 22, 2022
3ca5663
Melhorias na interface
iaglourenco Sep 30, 2022
db473ea
Adicionado testes do sintático. Testes OK
iaglourenco Oct 4, 2022
d10a9e5
Iniciado o desenvolvimento do Semantico e Tabela de simbolos
iaglourenco Oct 6, 2022
5d7aad9
Tabela de simbolos, semantico criados parcialmente
iaglourenco Oct 7, 2022
c6da07c
Merge remote-tracking branch 'origin/dev' into dev
iaglourenco Oct 7, 2022
20df937
Desempilha escopo ok
iaglourenco Oct 7, 2022
681836b
Avancos no pos Fixo
iaglourenco Oct 9, 2022
39ecf6b
Correção na contagem de colunas do lexico
iaglourenco Oct 9, 2022
bb0dd70
Melhorias na documentação
iaglourenco Oct 10, 2022
7d01999
Adicionado sistema de abas!
iaglourenco Oct 11, 2022
630e9cd
Avancos no semantico
iaglourenco Oct 11, 2022
2085faf
Analise semantica finalizada
iaglourenco Oct 12, 2022
9e1f329
Minimas melhorias na interface
iaglourenco Oct 12, 2022
b2b9d11
Comentários no código
iaglourenco Oct 19, 2022
4cf43b0
Analisa leia verifica tipo
iaglourenco Oct 25, 2022
a7dc21d
add varios arquivos e abre cada um em uma aba
fabioirokawa Oct 26, 2022
d350011
Remoção de compilação em massa, limpeza do código
iaglourenco Oct 28, 2022
c644014
Merge pull request #49 from iaglourenco/dev2
iaglourenco Oct 28, 2022
94c2f3f
Fix
iaglourenco Oct 28, 2022
1426492
Hotfix
iaglourenco Oct 28, 2022
09ef737
#48 concluido
iaglourenco Oct 28, 2022
a6322c8
Fix em msg de erro
iaglourenco Oct 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions compilador/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

<script src="codemirror/lib/codemirror.js"></script>
<script src="codemirror/addon/display/fullscreen.js"></script>
<script src="codemirror/addon/hint/show-hint.js"></script>
<script src="codemirror/addon/display/placeholder.js"></script>


<!-- Script imports -->
<script type="module" src="js/compilador.js"></script>
Expand Down Expand Up @@ -65,23 +66,28 @@ <h1 id="logo" class="logo">compi{<span class="logo_snip">LPD</span>}lador</h1>
<div class="menu_btn" id="abrir">
<span class="material-icons"> upload_file </span>
<span class="menu_btn_title" style="text-align: center">Carregar <br>(Ctrl+O)</span>
<input id="file-input" type="file" accept=".txt" />
<input id="file-input" type="file" accept=".txt" multiple/>
</div>
</div>
</header>

<div id="codeeditor">
<div id="codeeditor_header">
<div style="display: flex; align-items: baseline">
<legend>Editor:</legend>
<legend id="filename"></legend>
<div style="display: flex; align-items:center; justify-content:space-between">
<legend id="title">Editor:</legend>
<legend id="posicao">Ln 1, Col 1</legend>
</div>

<div id="tabs_container">


</div>
<legend id="posicao">Ln 1, Col 1</legend>
</div>

</div>

<div id="console">
<div style="display: flex; justify-content: space-between">
<div id="log_header">
<legend>Log</legend>
<legend id="limpar" onclick="document.getElementById('log').value=''">
Limpar (Ctrl+L)
Expand Down
211 changes: 160 additions & 51 deletions compilador/js/compilador.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
// Author: Iago Lourenço (iagojlourenco@gmail.com) / main.js
// Author: Iago Lourenço (iagojlourenco@gmail.com) / compilador.js

var filename = "myfile.txt";
import { ErroLexico, ErroSemantico, ErroSintatico } from "./erros.js";
// Import dos módulos do compilador
import sintatico from "./sintatico.js";
// Guarda as abas do editor
const tabs = [];
var editor;

var filename = "Novo.lpd";

function loadLocalFile(files, editor) {
async function loadLocalFile(files, editor) {
if (files.length == 1) {
var reader = new FileReader();
reader.fileName = files[0].name;
filename = files[0].name;
document.getElementById("filename").innerHTML = filename;

reader.onload = function (e) {
editor.setValue(e.target.result);
logar(`Arquivo ${e.target.fileName} carregado com sucesso!`);
createTab(e.target.fileName);
editor.setValue(e.target.result);
};
reader.readAsText(files[0]);
} else {
for (var i = 0; i < files.length; i++) {
var reader = new FileReader();
reader.fileName = files[i].name;
filename = files[i].name;

reader.onload = function (e) {
createTab(e.target.fileName);
editor.setValue(e.target.result);
logar(`Arquivo ${e.target.fileName} carregado com sucesso!`);
};
reader.readAsText(files[i]);
}
}
}

Expand Down Expand Up @@ -49,17 +70,136 @@ function logar(msg) {
document.getElementById("log").scrollHeight;
}

import { ErroLexico, ErroSemantico, ErroSintatico } from "./erros.js";
// Import dos módulos do compilador
import sintatico from "./sintatico.js";
function compileCode() {
var code = editor.getValue();
let log = document.getElementById("log");
log.value = "";
try {
var start = performance.now();
if (code.length > 0) {
logar(`Compilando...`);
// Chamada do sintático para iniciar a análise
const codigo = sintatico.iniciar(code);

logar(`SUCESSO!`);
} else {
throw new Error("Nenhum código inserido!");
}
} catch (e) {
console.error(e);
if (
e instanceof ErroLexico ||
e instanceof ErroSintatico ||
e instanceof ErroSemantico
) {
// Coloca o cursor na linha e coluna do erro
editor.setCursor(e.linha, e.coluna);
editor.focus();
}

// Printa a mensagem no log
logar(e.message);
} finally {
// Tempo de execução do compilador
var end = performance.now();
log.value += `\n\n---\nTempo de execução: ${
Math.round((end - start) * 100) / 100
}ms\n`;
}
}

// Tabs management functions
function createTab(name) {
/**
* Create a tab
*/
tabs.push({
doc: CodeMirror.Doc("", "text/x-lpd"),
name: name,
});
reconstructTabs();
addTabListeners();
activateTab(tabs.length - 1);
setTabName(tabs.length - 1, name);
}

function reconstructTabs() {
document.getElementById("tabs_container").innerHTML = "";
for (let i = 0; i < tabs.length; i++) {
document.getElementById("tabs_container").innerHTML += `
<div class="tab_c">
<div class="tab" id="tab${i}">
<span class="material-icons">code</span>
<span class="tab_title">${tabs[i].name}</span>
</div>
<span class="material-icons close" id="tab${i}_close">close</span>
</div>`;
}

document.getElementById(
"tabs_container"
).innerHTML += `<div id="new_tab"><span class="material-icons">add</span></div>`;

document.getElementById("new_tab").addEventListener("click", () => {
// create a new tab
createTab("Novo.lpd");
});
}
function activateTab(index) {
editor.swapDoc(tabs[index].doc);
editor.focus();
document.getElementById("posicao").innerHTML = `Ln ${
editor.getCursor().line + 1
}, Col ${editor.getCursor().ch + 1}`;

for (let j = 0; j < tabs.length; j++) {
document.getElementById(`tab${j}`).classList.remove("active");
}
document.getElementById(`tab${index}`).classList.add("active");
}
function setTabName(index, name) {
tabs[index].name = name;
}
function addTabListeners() {
for (let i = 0; i < tabs.length; i++) {
document.getElementById(`tab${i}`).addEventListener("click", () => {
activateTab(i);
});
document.getElementById(`tab${i}_close`).addEventListener("click", () => {
// Ask for confirmation
closeTab(i);
});
}
}

function closeTab(index) {
// Remove tab
if (tabs.length > 1) {
if (editor.getValue().length > 0) {
if (confirm("Deseja realmente fechar esta aba?")) {
tabs.splice(index, 1);
reconstructTabs();
addTabListeners();
activateTab(tabs.length - 1);
}
} else {
tabs.splice(index, 1);
reconstructTabs();
addTabListeners();
activateTab(tabs.length - 1);
}
} else {
logar("Não é possível fechar a última aba!");
}
}

window.onload = function () {
var editor = CodeMirror(document.getElementById("codeeditor"), {
editor = CodeMirror(document.getElementById("codeeditor"), {
mode: "text/x-lpd",
theme: "dracula",
lineNumbers: true,
autofocus: true,

placeholder: "Ctrl-O para abrir um arquivo, ou comece a digitar!",
viewportMargin: 150,
extraKeys: {
F11: function (cm) {
Expand All @@ -70,6 +210,15 @@ window.onload = function () {
},
},
});
tabs.push({ doc: editor.getDoc(), name: "Novo.lpd" });
reconstructTabs();
addTabListeners();
document.getElementById(`tab0`).classList.add("active");

logar("Bem vindo ao compilador LPD! aka. compi{LPD}lador");
logar(
"Para começar, carregue um arquivo ou digite o código no editor acima."
);

// Animação do logo
document.getElementById("logo").addEventListener("mouseover", function () {
Expand All @@ -83,9 +232,6 @@ window.onload = function () {

// Atualiza os números da linha e coluna na janela
editor.on("cursorActivity", function () {
if (document.getElementById("filename").value == "") {
document.getElementById("filename").value = filename;
}
document.getElementById("posicao").innerHTML = `Ln ${
editor.getCursor().line + 1
}, Col ${editor.getCursor().ch + 1}`;
Expand Down Expand Up @@ -125,44 +271,7 @@ window.onload = function () {

// Compila o código
document.getElementById("compilar").addEventListener("click", function () {
var code = editor.getValue();
let log = document.getElementById("log");
log.value = "";
try {
var start = performance.now();
if (code.length > 0) {
logar(`Compilando...`);
// Chamada do sintático para iniciar a análise
const codigo = sintatico.iniciar(code);

logar(`SUCESSO!`);
// logar(`Tokens: ${JSON.stringify(listaToken)}`);
// console.table(listaToken);
} else {
throw new Error("Nenhum código inserido!");
}
} catch (e) {
console.error(e);

if (
e instanceof ErroLexico ||
e instanceof ErroSintatico ||
e instanceof ErroSemantico
) {
// Coloca o cursor na linha e coluna do erro
editor.setCursor(e.linha - 1, e.coluna - 1);
editor.focus();
}

// Printa a mensagem no log
logar(e.message);
} finally {
// Tempo de execução do compilador
var end = performance.now();
log.value += `\n\n---\nTempo de execução: ${
Math.round((end - start) * 100) / 100
}ms\n`;
}
compileCode();
});

// Salva o arquivo
Expand Down
Loading