Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 33 additions & 0 deletions docs/Gramática.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
bloco_codigo: LBRACE expressão* RBRACE

expressão : FUNCTION IDENTIFIER LPAREN (IDENTIFIER (COMMA IDENTIFIER)*)? RPAREN bloco_codigo
: VAR|CONST IDENTIFIER EQ expressão
: expressão_co ((AND|OR) expressão_co)*

expressão_co: NOT expressão_co
: expressão_ar ((IGUAL|N_IGUAL|ME_QUE|MA_QUE|ME_IG_QUE|MA_IG_QUE) expressão_ar)*

expressão_ar: termo ((MAIS|MENOS) termo)*

termo : fator ((MULTIPLICAR|DIVIDIR) fator)*

fator : (MAIS|MENOS) fator
: power

power : call (POWER fator)*

call : atom (LPAREN (expressão (COMMA expressão)*)? RPAREN)?

atom : INT|FLOAT|STRING|IDENTIFIER
: LPAREN expressão RPAREN
: expressão_if
: expressão_fo
: expressão_wh

expressão_if: IF LPAREN expressão RPAREN bloco_codigo
(ELSEIF LPAREN expressão RPAREN bloco_codigo)*
(ELSE bloco_codigo)?

expressão_fo: FOR LPAREN IDENTIFIER EQ expressão TO expressão STEP expressão RPAREN bloco_codigo

expressão_wh: WHILE LPAREN expressão RPAREN bloco_codigo
19 changes: 19 additions & 0 deletions src/_examples/ProfMedia.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
var valor1 = 0
var valor2 = 0

print("Digite o valor 1 de um número inteiro")
read(valor1)

print("Digite o valor 2 de um número inteiro")
read(valor2)

//Processamento do programa
const resultado = valor1 + valor2

if(resultado >= 10) {
print("O valor é maior ou igual a 10. O resultado é " + resultado)
} else {
print("O valor é menor que 10. O resultado é " + resultado)
}
}
25 changes: 25 additions & 0 deletions src/_examples/ProfTabuada.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
var tabuada = 0
var nome = ""

escreva("Digite o seu nome")
leia(nome)

escreva("Tabuada que deseja calcular")
leia(tabuada)

resolverTabuada(tabuada)

escreva(nome + ". Foi resolvida a tabuada do " + tabuada)

function resolverTabuada(tabuada) {
var resto = 0

for(i = 0 to 20 step 1) {
if(resto == 0) {
escreva(tabuada + " X " + i + " = " + tabuada * i)
escreva(tabuada + " X " + i + " = " + tabuada * i)
}
}
}
}
16 changes: 0 additions & 16 deletions src/examples/SomaDoisNumeros.txt

This file was deleted.

9 changes: 0 additions & 9 deletions src/examples/teste.txt

This file was deleted.

16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Lexer } from "./models/Lexer";
import { Token } from "./models/Token";
import { ParserNode } from './parser/nodes/ParserNode';
import { CodeBlockNode } from './parser/nodes/CodeBlockNode';
import { Lexer } from "./lexer/Lexer";
import { Token } from "./lexer/Token";
import fs from 'fs';
import path from 'path';
import { Parser } from "./parser/Parser";

const FILENAME: string = 'examples/SomaDoisNumeros.txt';
const FILENAME: string = '_examples/ProfMedia.txt';

const filePath: string = path.join(__dirname, FILENAME);

Expand All @@ -12,7 +15,14 @@ fs.readFile(filePath, {encoding: 'utf-8'}, (err, data) => {
const lexer: Lexer = new Lexer(data);
const tokens: Token[] = lexer.findTokens();

console.log('Análise Léxica:');
console.table(tokens);

const parser: Parser = new Parser(tokens);
const codeBlock: ParserNode = parser.makeCodeBlock();

console.log('Análise Sintática:');
if (codeBlock != null) console.log(codeBlock.representation);
} else {
console.error(err);
}
Expand Down
35 changes: 23 additions & 12 deletions src/models/Lexer.ts → src/lexer/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ const CHARS: string[] = [...STARTING_CHARS, ...STARTING_DIGITS, '_'];
const STRING_INIT_SYMBOL: string = '"';
const STRING_STOP_SYMBOL: string = '"';
const COMMENT_INIT_SYMBOL: string = '//';
const COMMENT_STOP_SYMBOL: string = '\n';
const COMMENT_STOP_SYMBOL: string = ''
const END_OF_LINE_SYMBOL: string = '\n';

const SYMBOLS: string[] = ['\'', '*', '-', '+', '/', ';', '{', '}', '(', ')', '=', '|', '&', '%', '!', '<', '>'];
const SYMBOLS: string[] = ['\'', '*', '-', '+', '/', ';', '{', '}', '(', ')', '=', '|', '&', '%', '!', '<', '>', '^'];
const COMPOUND_SYMBOLS: string[] = [ '--', '++', '==', '===', '!=', '<=', '>=', '||', '&&'];

const KEYWORDS: string[] = ['break','return','continue','for','while', 'var', 'const', 'if', 'else', 'elseif', 'print', 'read'];
const IGNORED_TOKENS: string[] = ['\n', '\t', '\r', ' '];
const KEYWORDS: string[] = ['break','return','continue','for', 'to', 'step', 'while', 'var', 'const', 'function', 'if', 'else', 'elseif'];
const IGNORED_TOKENS: string[] = ['\t', '\r', ' '];

export class Lexer
{
Expand Down Expand Up @@ -52,6 +53,13 @@ export class Lexer
continue;
}

if(END_OF_LINE_SYMBOL === this.currentChar)
{
tokens.push(new Token(TokenType.END_OF_LINE, null));
this.advance();
continue;
}

if(IGNORED_TOKENS.includes(this.currentChar))
{
this.advance();
Expand Down Expand Up @@ -85,6 +93,8 @@ export class Lexer
this.error(LexerErrors.ILLEGAL_CHAR, `${this.currentChar} at position ${this.currentPosition}`);
}

tokens.push(new Token(TokenType.END_OF_FILE, null));

return tokens;
}

Expand Down Expand Up @@ -113,16 +123,10 @@ export class Lexer

private makeComment(): void
{
this.advance();

while(this.currentChar)
do
{
if(this.currentChar === COMMENT_STOP_SYMBOL) break;

this.advance();
}

if(this.currentChar === COMMENT_STOP_SYMBOL) this.advance();
} while(this.currentChar && this.currentChar !== END_OF_LINE_SYMBOL);
}

private makeIdentifier(): Token
Expand Down Expand Up @@ -211,6 +215,10 @@ export class Lexer
case '&': return SymbolType.AMPERSAND;
case '%': return SymbolType.PERCENTAGE;
case '!': return SymbolType.EXCLAMATION;
case '^': return SymbolType.POWER;
case ',': return SymbolType.COMMA;
case '>': return SymbolType.MORE_THAN;
case '<': return SymbolType.LESS_THAN;

case '==': return SymbolType.EQUALS;
case '===': return SymbolType.EXACTLY_EQUALS;
Expand All @@ -234,9 +242,12 @@ export class Lexer
case 'return': return KeywordType.RETURN;
case 'continue': return KeywordType.CONTINUE;
case 'for': return KeywordType.FOR;
case 'to': return KeywordType.TO;
case 'step': return KeywordType.STEP;
case 'while': return KeywordType.WHILE;
case 'var': return KeywordType.VAR;
case 'const': return KeywordType.CONST;
case 'function': return KeywordType.FUNCTION;
case 'if': return KeywordType.IF;
case 'else': return KeywordType.ELSE;
case 'elseif': return KeywordType.ELSEIF;
Expand Down
2 changes: 1 addition & 1 deletion src/models/LexerError.ts → src/lexer/LexerError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class LexerError

public asString(): string
{
return ` ${this.name}: ${this.details}`;
return `[LEXER] ${this.name}: ${this.details}`;
}
}

Expand Down
16 changes: 14 additions & 2 deletions src/models/Token.ts → src/lexer/Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export class Token
this.type = type;
this.subType = subType || null;
}

public get deepType(): string
{
return this.subType ? this.subType : this.type;
}
}

export class TokenType
Expand All @@ -21,6 +26,8 @@ export class TokenType
public static readonly FLOAT: string = 'FLOAT';
public static readonly STRING: string = 'STRING';
public static readonly IDENTIFIER: string = 'IDENTIFIER';
public static readonly END_OF_LINE: string = 'EOL';
public static readonly END_OF_FILE: string = 'EOF';
}

export class SymbolType
Expand All @@ -40,6 +47,10 @@ export class SymbolType
public static readonly AMPERSAND: string = 'SYMBOL_AMPERSAND';
public static readonly PERCENTAGE: string = 'SYMBOL_PERCENTAGE';
public static readonly EXCLAMATION: string = 'SYMBOL_EXCLAMATION';
public static readonly POWER: string = 'SYMBOL_POWER';
public static readonly COMMA: string = 'SYMBOL_COMMA';
public static readonly MORE_THAN: string = 'SYMBOL_MORE_THAN';
public static readonly LESS_THAN: string = 'SYMBOL_LESS_THAN';

public static readonly EQUALS: string = 'SYMBOL_EQUALS';
public static readonly EXACTLY_EQUALS: string = 'SYMBOL_EXACTLY_EQUALS';
Expand All @@ -58,12 +69,13 @@ export class KeywordType
public static readonly RETURN: string = 'KEYWORD_RETURN';
public static readonly CONTINUE: string = 'KEYWORD_CONTINUE';
public static readonly FOR: string = 'KEYWORD_FOR';
public static readonly TO: string = 'KEYWORD_TO';
public static readonly STEP: string = 'KEYWORD_STEP';
public static readonly WHILE: string = 'KEYWORD_WHILE';
public static readonly VAR: string = 'KEYWORD_VAR';
public static readonly CONST: string = 'KEYWORD_CONST';
public static readonly FUNCTION: string = 'KEYWORD_FUNCTION';
public static readonly IF: string = 'KEYWORD_IF';
public static readonly ELSE: string = 'KEYWORD_ELSE';
public static readonly ELSEIF: string = 'KEYWORD_ELSEIF';
public static readonly PRINT: string = 'KEYWORD_PRINT';
public static readonly READ: string = 'KEYWORD_READ';
}
Loading