Skip to content

Commit 8435ba7

Browse files
committed
add format example
1 parent d294953 commit 8435ba7

File tree

8 files changed

+91
-7
lines changed

8 files changed

+91
-7
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html}
2+
uses
3+
sysutils;
4+
5+
var
6+
s : string;
7+
begin
8+
// Explicit argument indices may be used to re-order output.
9+
s := format('%4$2s %3$2s %2$2s %1$2s', 'a', 'b', 'c', 'd');
10+
// -> ' d c b a'
11+
WriteLn(s);
12+
13+
s := format('pi = %+10.4f', pi);
14+
// -> 'e = +2,7183'
15+
WriteLn(s);
16+
17+
// The '(' numeric flag may be used to format negative numbers with
18+
// parentheses rather than a minus sign. Group separators are
19+
// automatically inserted.
20+
s := format('Amount gained or lost since last statement: $ %(,.2f',
21+
balanceDelta);
22+
// -> 'Amount gained or lost since last statement: $ (6,217.58)'
23+
writeln(s);
24+
end.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Program TimeToStr;
2+
3+
{ This program demonstrates the Time function }
4+
5+
Uses sysutils;
6+
7+
Begin
8+
Writeln ('The time is : ', TimeToStr(Time));
9+
End.

libCompiler/src/main/java/com/duy/pascal/backend/ast/expressioncontext/ExpressionContextMixin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,8 @@ public DeclaredType getTypedefTypeLocal(String identifer) {
564564
* define custom type
565565
*/
566566
public void declareTypedef(String name, DeclaredType type) {
567-
typedefs.put(name, type);
568-
mListNameTypes.add(new InfoItem(StructureType.TYPE_DEF, name));
567+
typedefs.put(name.toLowerCase(), type);
568+
mListNameTypes.add(new InfoItem(StructureType.TYPE_DEF, name.toLowerCase()));
569569
}
570570

571571
public void declareTypedefs(String name, List<DeclaredType> types) {

libCompiler/src/main/java/com/duy/pascal/backend/builtin_libraries/SysUtilsLibrary.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import com.duy.pascal.backend.builtin_libraries.annotations.PascalMethod;
2222
import com.duy.pascal.backend.builtin_libraries.runtime_exceptions.EConvertError;
2323
import com.duy.pascal.backend.runtime_exception.RuntimePascalException;
24+
import com.duy.pascal.backend.types.BasicType;
2425

2526
import java.text.DecimalFormat;
27+
import java.util.Date;
2628
import java.util.Map;
2729

2830
/**
@@ -54,7 +56,7 @@ public void declareConstants(ExpressionContextMixin parentContext) {
5456

5557
@Override
5658
public void declareTypes(ExpressionContextMixin parentContext) {
57-
59+
parentContext.declareTypedef("TDateTime", BasicType.Long);
5860
}
5961

6062
@Override
@@ -279,8 +281,19 @@ public StringBuilder AnsiLowerCase(StringBuilder s1) throws EConvertError {
279281
}
280282

281283

282-
@PascalMethod(description = "Format a string with given arguments.")
284+
@PascalMethod(description = "Format a string with given arguments. (Java)")
283285
public StringBuilder format(String format, Object[] arg) {
284286
return new StringBuilder(String.format(format, arg));
285287
}
288+
289+
@PascalMethod(description = "Convert a TDateTime time to a string using a predefined format")
290+
public StringBuilder TimeToStr(Long time) {
291+
Date date = new Date(time);
292+
return new StringBuilder(date.toString());
293+
}
294+
295+
@PascalMethod(description = "Returns the current time.")
296+
public Long time() {
297+
return System.currentTimeMillis();
298+
}
286299
}

libCompiler/src/main/java/com/duy/pascal/frontend/code_sample/activities/CodeSampleActivity.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
import com.duy.pascal.frontend.activities.AbstractAppCompatActivity;
3131
import com.duy.pascal.frontend.activities.ExecuteActivity;
3232
import com.duy.pascal.frontend.code.CompileManager;
33-
import com.duy.pascal.frontend.editor.EditorActivity;
34-
import com.duy.pascal.frontend.code_sample.fragments.FragmentCodeSample;
3533
import com.duy.pascal.frontend.code_sample.adapters.CodePagerAdapter;
3634
import com.duy.pascal.frontend.code_sample.adapters.CodeSampleAdapter;
35+
import com.duy.pascal.frontend.code_sample.fragments.FragmentCodeSample;
36+
import com.duy.pascal.frontend.editor.EditorActivity;
3737
import com.duy.pascal.frontend.file.ApplicationFileManager;
3838
import com.google.firebase.analytics.FirebaseAnalytics;
3939
import com.miguelcatalan.materialsearchview.MaterialSearchView;
@@ -54,7 +54,8 @@ public class CodeSampleActivity extends AbstractAppCompatActivity implements Cod
5454
private CodePagerAdapter pagerAdapter;
5555

5656
public CodeSampleActivity() {
57-
categories = new String[]{"Basic", "System", "Crt", "Dos", "Graph", "Math", "Android",
57+
categories = new String[]{"Basic", "System", "Crt", "Dos", "Graph", "Math",
58+
"Complete_Program", "Android",
5859
"Android_Dialog", "Android_ZXing", "Android_Location"};
5960
}
6061

libCompiler/src/test/java/com/duy/pascal/lib/SysUtilsTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,8 @@ public String getDirTest() {
3333
public void testAll() {
3434
runAll();
3535
}
36+
37+
public void testTime() {
38+
run("time.pas");
39+
}
3640
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html}
2+
uses
3+
sysutils;
4+
5+
var
6+
s : string;
7+
begin
8+
// Explicit argument indices may be used to re-order output.
9+
s := format('%4$2s %3$2s %2$2s %1$2s', 'a', 'b', 'c', 'd');
10+
// -> ' d c b a'
11+
WriteLn(s);
12+
13+
s := format('pi = %+10.4f', pi);
14+
// -> 'e = +2,7183'
15+
WriteLn(s);
16+
17+
// The '(' numeric flag may be used to format negative numbers with
18+
// parentheses rather than a minus sign. Group separators are
19+
// automatically inserted.
20+
s := format('Amount gained or lost since last statement: $ %(,.2f',
21+
balanceDelta);
22+
// -> 'Amount gained or lost since last statement: $ (6,217.58)'
23+
writeln(s);
24+
end.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Program Example23;
2+
3+
{ This program demonstrates the Time function }
4+
5+
Uses sysutils;
6+
7+
Begin
8+
Writeln ('The time is : ', TimeToStr(Time));
9+
End.

0 commit comments

Comments
 (0)