Example gallery
The NeoObjectPascal repository includes a collection of ready-to-run examples in NeoObjectPascal/examples/, numbered from 01 to 44. They range from "Hello, World!" to object orientation, tests with mocking, and cloud execution. This page organizes them by theme and shows the code of a few representative ones.
How to run
Run any example by passing the file path to the interpreter JAR:
java -jar neoobjectpascal.jar examples/01-OlaMundo.npasTest files use the .test.npas extension and run in test mode:
# um arquivo de teste
java -jar neoobjectpascal.jar -t examples/34-TesteSimples.test.npas
# todos os testes de um diretório (recursivo, com cobertura)
java -jar neoobjectpascal.jar --test-all examples/TIP
The actual JAR name depends on your build (for example, target/neoobjectpascal-*.jar). See the Introduction to generate the interpreter.
Fundamentals
| File | Demonstrates |
|---|---|
01-OlaMundo.npas | The minimal program: a single WriteLn in the main block |
02-Variaveis.npas | Variable declaration (String, Integer) and assignment |
15-ConcatenacaoStrings.npas | String concatenation with + and reading input |
16-ExemploOriginal.npas | Introductory example combining several features |
begin
WriteLn("Olá, Mundo!");
end.Olá, Mundo!
Control flow and functions
| File | Demonstrates |
|---|---|
03-EstruturasDeControle.npas | if ... then and the for i := 1 to N do loop |
04-Funcoes.npas | Function declaration and call with return |
07-ProgramacaoFuncional.npas | Chained pipe operator |> |
function somar(a, b): Integer;
begin
return a + b;
end;
var resultado: Integer;
begin
resultado := somar(20, 22);
WriteLn("O resultado da soma é: ", resultado);
end.O resultado da soma é: 42
The pipe operator passes the left-hand value as the first argument of the function on the right:
function dobrar(x: Integer): Integer;
begin
return x * 2;
end;
function adicionarUm(x: Integer): Integer;
begin
return x + 1;
end;
begin
WriteLn("O resultado do pipeline é: ", 5 |> dobrar |> adicionarUm);
end.O resultado do pipeline é: 11
Data and I/O
| File | Demonstrates |
|---|---|
05-JSON.npas | JSON.parse(...) into to load an object |
06-CSV.npas | CSV.parse(...) into to load rows |
08-EntradaSaida.npas | WriteLn and showMenu |
13-MenuInterativo.npas | Numbered menu with showMenu |
14-EntradaUsuario.npas | User input with ReadLn |
var dados: Object;
var jsonString: String;
begin
jsonString := '{"nome": "Carmen Sandiego", "idade": 30, "cidade": "São Paulo"}';
JSON.parse(jsonString) into dados;
WriteLn("Dados JSON processados com sucesso!");
end.Dados JSON processados com sucesso!
Modules and internal libraries
| File | Demonstrates |
|---|---|
09-Modulos.npas | uses of a local .npas module |
10-ModulosCompleto.npas | Using multiple modules |
11-ProjetoHierarquico.npas | Modules in hierarchical folders (lib.core.calculadora) |
17-BibliotecasInternas.npas | Functions from internal.math, internal.string, internal.datetime |
26-TesteDatetime.npas | The internal.datetime library |
28-TesteFile.npas / 29-TesteFileSimples.npas | The internal.file library |
uses internal.math, internal.string;
begin
WriteLn("Fatorial de 5: ", factorial(5));
WriteLn("Fibonacci(8): ", fibonacci(8));
WriteLn("Maiúsculas: ", toUpperCase("neoobjectpascal"));
end.Details of all the functions in Internal libraries.
Java and hybrid systems
| File | Demonstrates |
|---|---|
20-SistemaHibrido.npas | Combining Pascal code and embedded Java |
21-TesteHibridoSimples.npas | A simplified version of the hybrid system |
22-TesteSemJava.npas | The equivalent without Java for comparison |
23-DebugJava.npas | Debugging Java blocks |
24-FuncaoJava.npas | A function that returns a value from a Java block |
25-JavaComParametros.npas | A Java block receiving parameters (param0, param1, ...) |
27-SistemaHibridoFinal.npas | The complete hybrid system |
function testeJava(): String
begin
return java:() {
return "Olá do Java!";
};
end;
begin
WriteLn("Resultado: ", testeJava());
end.Resultado: Olá do Java!
More in Java integration.
Object orientation
| File | Demonstrates |
|---|---|
30-ClasseSimples.npas | A class with fields, constructor Create and a method |
31-Heranca.npas | Inheritance with extends, virtual/override |
32-Interface.npas | interface and implements |
33-Polimorfismo.npas | Polymorphism with overridden methods |
class Pessoa
var nome: String;
var idade: Integer;
constructor Create(n: String, i: Integer)
begin
self.nome := n;
self.idade := i;
end;
public function apresentar(): String
begin
return "Olá, meu nome é " + self.nome + " e tenho " + self.idade + " anos.";
end;
end;
var pessoa: Pessoa;
begin
pessoa := new Pessoa("João", 30);
WriteLn(pessoa.apresentar());
end.Olá, meu nome é João e tenho 30 anos.
Full guide in Classes, Inheritance and Interfaces.
Testing and mocking
| File | Demonstrates |
|---|---|
18-TesteSimples.npas / 19-TesteCompleto.npas | Introductory test blocks |
34-TesteSimples.test.npas | GIVEN-WHEN-THEN pattern with expect(...).toBe(...) |
35-TesteHeranca.test.npas | Inheritance tests |
36-TesteInterface.test.npas | Interface tests |
37-TestePolimorfismo.test.npas | Polymorphism tests |
38-TesteMocking.test.npas | mock ... thenReturn and verify |
class Calculadora
public function somar(a: Integer, b: Integer): Integer
begin
return a + b;
end;
end;
test "Calculadora deve somar dois números corretamente"
begin
var calc: Calculadora;
calc := new Calculadora();
var resultado: Integer;
resultado := calc.somar(5, 3);
expect(resultado).toBe(8);
end;Replace a dependency with a mock and then verify it was called:
test "NotificadorUsuario deve chamar EmailService ao notificar"
begin
var emailService: EmailService;
emailService := new EmailService();
mock EmailService enviarEmail thenReturn true;
var notificador: NotificadorUsuario;
notificador := new NotificadorUsuario(emailService);
var resultado: Boolean;
resultado := notificador.notificar("usuario@teste.com");
expect(resultado).toBeTrue();
verify EmailService enviarEmail;
end;Full guide in Unit testing.
Debugging and cloud
| File | Demonstrates |
|---|---|
39-DebugSimples.npas | A simple program for the interactive debugger (-d) |
40-CloudExample.npas | An example to run on NeoObjectPascal Cloud |
debug-boolean.npas, debug-class.npas, debug-class2.npas | Auxiliary debugging scenarios |
New features
Examples that exercise recent language features:
| File | Demonstrates |
|---|---|
41-OperadoresBooleanos.npas | The and, or, not operators in conditions |
42-AritmenticaReal.npas | Arithmetic with Real and auto-promotion of Integer |
43-Arrays.npas | Array literals, 0-based indexing, element assignment and for ... in |
44-TratamentoErros.npas | try/catch/finally and raise |
var numeros: Array;
var item: Integer;
var soma: Integer;
begin
numeros := [10, 20, 30, 40, 50];
numeros[2] := 99; // atribuição de elemento (base 0)
soma := 0;
for item in numeros do
soma := soma + item;
WriteLn("Soma: ", soma);
end.Soma: 219
Error handling with try/catch/finally and raise:
function validarIdade(idade: Integer): String
begin
if idade < 0 then
raise "Idade invalida: " + idade;
return "Idade valida: " + idade;
end;
begin
try
begin
WriteLn(validarIdade(0 - 5));
end
catch (erro)
begin
WriteLn("Capturado: ", erro);
end
finally
begin
WriteLn("Cleanup concluido");
end;
end.Capturado: Idade invalida: -5 Cleanup concluido
Keep exploring
New here? Start with the Introduction to install the interpreter and run your first program. For the complete syntax, see the Language reference.