Skip to content

Commit

Permalink
GH-392 Add log structure (Resolves #399)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Nov 2, 2019
1 parent 185ed62 commit 0696215
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 49 deletions.
2 changes: 1 addition & 1 deletion examples/hello_world.panda
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module example

main {
Console.print("Hello Panda")
log 'Hello Panda'
}
4 changes: 2 additions & 2 deletions examples/module_manager/app.panda
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ require panda-dependency

main {
// print panda-dependency message
System.out.println(PandaDependency.getMessage())
log PandaDependency.getMessage()

// print subdependency (defined in panda-dependency/panda.hjson) message
System.out.println(LDDependency.getMessage())
log LDDependency.getMessage()
}
2 changes: 1 addition & 1 deletion examples/pandasite/index.panda
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module awesomeness

class Panda {
shared hello(String name) {
Console.print("Hey, " + name + ". You're finally awake ฅ^•ﻌ•^ฅ ")
log "Hey, " + name + ". You're finally awake ฅ^•ﻌ•^ฅ "
}
}

Expand Down
56 changes: 22 additions & 34 deletions examples/tests/current_test.panda
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,12 @@ require java:collections // import submodules by default? :thinking:
// import java class
import org.panda_lang.panda.PandaConstants

// define public Console class that will allow us to proxy logs
public class Console {

// define public static method that accepts multiple arguments (varargs)
public static log(Object... values) {
// print values using the java output, System class is in the 'java' module which is imported by default
System.out.println(StringUtils.toString(values))
}

}

// main statement is called when the script is launched
main {
// create Test instance, then call its method and finally print content of testField
Test test = new Test('"Constructor"')
test.echo("Method echo")
Console.log(test.getTestField().testField)
log test.getTestField().testField

// define variable init that may be initialized later
late String init
Expand All @@ -49,16 +38,15 @@ main {
// compare values and assign result to the flag variable
Bool flag = i == 1

// print some recent values in console
Console.log("Hello Panda", flag, varFoo, s, test, i, math)

// print some recent values in the console
log 'Hello Panda', flag, varFoo, s, test, i, math

// verify logic
if !flag {
Console.log("Nope")
log 'Nope'
}
else {
Console.log("Yay") // expected
log 'Yay' // expected
}

// assign ArrayList (class) value to List (interface) variable and add some values
Expand Down Expand Up @@ -86,7 +74,7 @@ main {

// iterate as long as flag is not equal to the false
while flag != false {
Console.log(builder.toString())
log builder.toString()
break // or just stop
}

Expand All @@ -95,19 +83,19 @@ main {
testArray.modify(test)

// print array and some logical expressions
Console.log("Hello " + "Array " + testArray.array.toString())
Console.log("OR v AND: " + (false || false) + ", " + false || true, true && false, true && true)
Console.log("Compare: " + 1 > 2, 1 > 2, 1 < 2, 2 < 1)
Console.log("Random", (false || false) + ", " + false || true, true && false, true && true, (false || false) + ", " + false || true, true && false, true && true)
log "Hello " + "Array " + testArray.array.toString()
log "OR v AND: " + (false || false) + ", " + false || true, true && false, true && true
log "Compare: " + 1 > 2, 1 > 2, 1 < 2, 2 < 1
log "Random", (false || false) + ", " + false || true, true && false, true && true, (false || false) + ", " + false || true, true && false, true && true

// test (in/de)crease operations
mut Int creaseValue = 10
Console.log("Increase: " + creaseValue++, creaseValue++, ++creaseValue, ++creaseValue)
Console.log("Decrease: " + creaseValue--, creaseValue--, --creaseValue, --creaseValue)
log "Increase: " + creaseValue++, creaseValue++, ++creaseValue, ++creaseValue
log "Decrease: " + creaseValue--, creaseValue--, --creaseValue, --creaseValue

// increase expression as standalone statement
creaseValue++
Console.log(creaseValue)
log creaseValue

// create instance of class imported from another file
Required required = new Required()
Expand All @@ -117,28 +105,28 @@ main {
try {
throw new RuntimeException("Try")
} catch (Exception e) {
Console.log(e.getMessage()) // gotcha
log e.getMessage() // gotcha
}

// print constant from imported java class
Console.log(PandaConstants.VERSION)
log PandaConstants.VERSION

// test standard fori loop
for (mut Int index = 3; index != 0; index--) {
if index == 2 {
continue
}

Console.log("ForI: " + index)
log "ForI: " + index
}

// print variable declared by the expression :0
Console.log("Assignation: " + (Object assignation = 10))
log "Assignation: " + (Object assignation = 10)

// check if variable defined in expr instanceof integer
if assignation is Int {
Int castTest = assignation as Int // and cast that value to integer
Console.log("Is int value: " + castTest)
log "Is int value: " + castTest
}

// Make sure that here is no way to create instance of interface
Expand All @@ -161,7 +149,7 @@ shared class Foo : Test, IEcho {

// we need to mark overridden methods by the keyword
override shared static anotherEcho(String message1) {
Console.log(message1)
log message1
}

// we need to impl all methods
Expand All @@ -182,12 +170,12 @@ class Test {

// constructor prints random message and assigns itself to testField field
constructor(String message2) {
Console.log(message2)
log message2
this.testField = this
}

shared echo(Object message3) {
Console.log(message3)
log message3
}

// get test instance
Expand All @@ -213,7 +201,7 @@ local class TestArray {
this.getArray()[Test.INDEX] = "Hello Array"
array[6] = String.valueOf(this)

Console.log("Value at array[test.index]: " + this.getArray()[test.INDEX])
log "Value at array[test.index]: " + this.getArray()[test.INDEX]
}

// get array
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/current_test_required.panda
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export org.panda_lang.utilities.commons.StringUtils
shared class Required {

shared hello() {
Console.log("Required print")
log "Required print"

// Imported by current_test.panda java class should not be visible
// Console.print(PandaConstants.VERSION)
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/performance/matmul.panda
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ main {
Double[][] b = m.matgen(n)
Double[][] x = m.matmul(a, b)

System.out.println(x[n / 2][n / 2])
log x[n / 2][n / 2]
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void updateWorkers() {
public Optional<ExpressionTransaction> parseSilently(Context context, Streamable streamable, ExpressionParserSettings settings) {
try {
return Optional.of(parse(context, streamable, settings));
} catch (Exception e) {
} catch (PandaExpressionParserException e) {
return Optional.empty();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2015-2019 Dzikoysk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.panda_lang.framework.language.interpreter.pattern.custom.elements;

import org.panda_lang.framework.design.interpreter.parser.Components;
import org.panda_lang.framework.design.interpreter.parser.Context;
import org.panda_lang.framework.design.interpreter.parser.expression.ExpressionParser;
import org.panda_lang.framework.design.interpreter.parser.expression.ExpressionTransaction;
import org.panda_lang.framework.language.interpreter.parser.expression.PandaExpressionParserException;
import org.panda_lang.framework.language.interpreter.pattern.custom.CustomPatternElementBuilder;
import org.panda_lang.framework.language.interpreter.pattern.custom.UniversalData;
import org.panda_lang.framework.language.resource.syntax.separator.Separators;

import java.util.ArrayList;
import java.util.Collection;

public final class ArgumentsElement extends CustomPatternElementBuilder<ExpressionTransaction[], ArgumentsElement> {

protected ArgumentsElement(String id) {
super(id);

super.custom(((data, source) -> {
Context context = data.get(UniversalData.CONTEXT);
ExpressionParser parser = context.getComponent(Components.EXPRESSION);
Collection<ExpressionTransaction> transactions = new ArrayList<>();

while (source.hasNext()) {
try {
transactions.add(parser.parse(context, source));
} catch (PandaExpressionParserException e) {
break;
}

if (source.hasNext()) {
if (!source.getNext().equals(Separators.COMMA)) {
break;
}

source.next();
}
}

return transactions.toArray(new ExpressionTransaction[0]);
}));
}

public static ArgumentsElement create(String id) {
return new ArgumentsElement(id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public final class Keywords {

public static final Keyword LOCAL = new Keyword("local");

public static final Keyword LOG = new Keyword("log");

public static final Keyword LOOP = new Keyword("loop");

public static final Keyword MAIN = new Keyword("main");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@

@Target({ ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Autowired {
public @interface Inject {

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.panda_lang.utilities.commons.ReflectionUtils;
import org.panda_lang.utilities.inject.annotations.Autowired;
import org.panda_lang.utilities.inject.annotations.Inject;
import org.panda_lang.utilities.inject.annotations.Injectable;
import org.panda_lang.utilities.inject.annotations.Wired;

Expand Down Expand Up @@ -64,18 +64,18 @@ void testInjector() {

private static class TestClass {

@Autowired
@Inject
TestClass(String value) {
Assertions.assertEquals(HELLO, value);
}

@Autowired
@Inject
public String testTypeInvoke(String value) {
Assertions.assertEquals(HELLO, value);
return value;
}

@Autowired
@Inject
private String testAnnotationInvoke(@CustomAnnotation("hello-autowired") String value) {
Assertions.assertEquals(HELLO_AUTOWIRED, value);
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ private T delegateNext(Context context, InjectorController controller, Injector
throw new BootstrapException("Error occurred: " + e.getMessage(), e.getTargetException());
}
catch (Exception e) {
// e.printStackTrace();
throw e;
}
catch (Throwable throwable) {
throwable.printStackTrace();
// throwable.printStackTrace();
throw new BootstrapException("Internal error: " + (throwable.getMessage() != null ? throwable.getMessage() : throwable.getClass()), throwable);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.panda_lang.panda.language.interpreter.parser.prototype.ParameterParser;
import org.panda_lang.panda.language.interpreter.parser.prototype.PrototypeParser;
import org.panda_lang.panda.language.interpreter.parser.scope.LateDeclarationParser;
import org.panda_lang.panda.language.interpreter.parser.scope.LogParser;
import org.panda_lang.panda.language.interpreter.parser.scope.StandaloneExpressionParser;
import org.panda_lang.panda.language.interpreter.parser.scope.TryCatchParser;
import org.panda_lang.panda.language.interpreter.parser.scope.block.conditional.ConditionalBlockParser;
Expand Down Expand Up @@ -77,6 +78,7 @@ public final class PandaParsers {
// scope
// AssignationParser.class, off
LateDeclarationParser.class,
LogParser.class,
StandaloneExpressionParser.class,
TryCatchParser.class,

Expand Down
Loading

0 comments on commit 0696215

Please sign in to comment.