Skip to content

Commit 4274bb2

Browse files
authored
Merge pull request #9 from BeyondMagic/analise_semantica
2 parents b3a4cfb + 6295d0e commit 4274bb2

File tree

15 files changed

+125
-4
lines changed

15 files changed

+125
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Resumo do MVP de linguagem suportada:
88
- I/O: mapeamento simples printf/puts -> print; scanf opcional.
99
- Saída Lua: usar local para variáveis, funções Lua equivalentes, operadores com mesma semântica; arrays opcionais como tabelas.
1010

11+
O que não será suportado:
12+
- Ponteiros, structs, alocação dinâmica, manipulação de memória.
13+
- Recursos avançados: pré-processador, macros, diretivas de compilação.
14+
1115
```
1216
.
1317
├── lexer/ # regras léxicas (lexer.l)

scripts/run_semantic_tests.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
COMPILER_CMD="${COMPILER_CMD:-compiler --semantic}"
5+
6+
RED=$(printf '\033[31m')
7+
GRN=$(printf '\033[32m')
8+
YEL=$(printf '\033[33m')
9+
RST=$(printf '\033[0m')
10+
11+
fail_count=0
12+
pass_count=0
13+
14+
echo "== PASS cases =="
15+
for f in tests/semantic/pass/*.c; do
16+
if [ ! -e "$f" ]; then continue; fi
17+
if $COMPILER_CMD "$f" >/dev/null 2>tmp.err; then
18+
echo -e "${GRN}PASS${RST} $f"
19+
pass_count=$((pass_count+1))
20+
else
21+
echo -e "${RED}FAIL (unexpected error)${RST} $f"
22+
cat tmp.err
23+
fail_count=$((fail_count+1))
24+
fi
25+
done
26+
27+
echo ""
28+
echo "== FAIL cases =="
29+
for f in tests/semantic/fail/*.c; do
30+
base=$(basename "$f")
31+
golden="tests/golden/fail/${base}.golden"
32+
if $COMPILER_CMD "$f" >/dev/null 2>tmp.err; then
33+
echo -e "${RED}FAIL (expected error, got success)${RST} $f"
34+
fail_count+=1
35+
else
36+
if diff -u "$golden" tmp.err >/dev/null; then
37+
echo -e "${GRN}PASS (matched golden)${RST} $f"
38+
pass_count=$((pass_count+1))
39+
else
40+
echo -e "${YEL}MISMATCH${RST} $f"
41+
echo "Expected:"
42+
cat "$golden" || true
43+
echo "Got:"
44+
cat tmp.err
45+
fail_count=$((fail_count+1))
46+
fi
47+
fi
48+
done
49+
50+
rm -f tmp.err || true
51+
52+
echo ""
53+
echo "Summary: ${GRN}${pass_count} passed${RST}, ${RED}${fail_count} failed${RST}."
54+
if [ "$fail_count" -ne 0 ]; then exit 1; fi

tests/expressions.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
int main()
1+
int add(int k, int y)
2+
{
3+
return k + y;
4+
}
5+
6+
int sum(int a, int b)
7+
{
8+
// a + b
9+
/*
10+
* comment
11+
*/
12+
int r = (a + b) / 10;
13+
return r;
14+
}
15+
16+
int main(int K)
217
{
318
int a = 10;
419
int b = 20;
5-
int sum = a + b;
6-
printf("Sum: %d\n", sum);
20+
int r = sum(a, b) + add(a, b);
721
return 0;
8-
}
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
array_index_string.c:2:11: E003: índice de array não numérico (string)

tests/golden/fail/bad_if.c.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bad_if.c:1:5: E002: condição não booleana em if (use comparação explícita)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int_plus_string.c:2:11: E001: tipo inválido em operação aritmética: int + string
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
wrong_assign.c:2:1: E004: atribuição incompatível: float ← int*
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int main ()
2+
{
3+
int arr[2];
4+
int x = arr["0"]; // E003: índice de array não numérico
5+
return 0;
6+
}

tests/semantic/fail/bad_if.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int main ()
2+
{
3+
if ("texto") { } // E002: condição não booleana em if (use comparação explícita)
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
int main ()
2+
{
3+
int a = 1; char* s = "x";
4+
int z = a + s; // E001: tipo inválido em operação aritmética
5+
}

0 commit comments

Comments
 (0)